How do I get the options for bundled products on the success page? - magento

On the success page, I have no trouble getting a list of the products purchased with the following code:
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
foreach ($order->getAllItems() as $item){
$subtotal = number_format($item->getSubtotal(),2);
}
What I can't figure out, is how to get an object or an array of the options for bundled products. These are standard options like what color a product is.

I have not specifically tried this with bundled products, but the code below works with configurable products, and I'm sure you can modify it as needed to fit your situation.
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
foreach ($order->getAllItems() as $item){
$productOptions = $item->getProductOptions();
if (isset($productOptions['attributes_info'])) {
foreach ($productOptions['attributes_info'] as $productOption) {
echo $label = $productOption['label'];
echo '<br />';
echo $value = $productOption['value'];
}
}
}
My suggestion is to start broad (i.e. at the $item level), see what Magento returns (using Zend_Debug::dump($item->getData()), and then work your way down to what you need.
Hope that helps.

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

How to remove item from quote in Magento?

During the checkout process I sometimes want to programmatically remove items from the session's quote. So I tried this code:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$all_quote_items = $quote->getAllItems();
foreach ($all_quote_items as $item) {
$quote->removeItem($item->getId())->save();
}
However, after this loop the list of items in the $quote object is still the same, i.e. no items have been removed.
Any ideas what I am missing here?
Using Magento 1.4.1.1
Try
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item)
{
$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();
}
See http://www.magentocommerce.com/boards/viewthread/30113/
In Magento 1.7.0.0 version, you can use:
Mage::getSingleton('checkout/cart')->truncate()->save();
I do a similar process while looking for items of a certain type, The logic I applied is:
$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach ($cartItems as $item)
{
$quote->removeItem($item->getId())->save();
}
Try the above and if that fails I would start dumping the quote objects out before and after this logic is executed to see what differences there are.
Try the below code It will work
$product = $observer->getEvent()->getProduct();
$cart = Mage::getSingleton('checkout/cart');
foreach ($cart->getQuote()->getItemsCollection() as $_item) {
if ($_item->getProductId() == $productId) {
$_item->isDeleted(true);
//Mage::getSingleton('core/session')->addNotice('This product cannot be added to shopping cart.');
}
}

Magento - get cart items for a given product id

I try to get the cart items for a given product;
I have tried this code :
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load('2784');
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$cartItems = $quote->getItemByProduct($product);
foreach ($cartItems as $item) {
echo $item->getId()."<br/>";
}
but it don't gives anything.
How can I modify my code to use "getItemByProduct" in the right form ?
Thanks for help.
getItemByProduct() returns the first matching Mage_Sales_Model_Quote_Item so there is no need for an extra loop.
$item = $quote->getItemByProduct($product);
if ($item !== false) echo $item->getId();
I'd use
foreach ($quote->getItems() as $item) {
if ($item->getProductId() == $product->getId()) {
print_r($item->getData());
}
}
You can not use getItemByProduct() function on the checkout/cart or checkout/quote model as that method is of the sales/quote model.
You can find this method at the Mage_Sales_Model_Quote class. so it is used withe sales/quote. hope this is useful.

Magento: Filter products by Status

I'm having some serious Magento issues here. As expected the following:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 1));
Will return all enabled products for my $category_id. However this:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 0));
Does not return disabled products. I can't seem to find a way to return disabled products, and I don't know why.
I've tried this:
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Which was meant to have worked, but apparently may have been deprecated.
Does anyone know how to get all products in a category, enabled and disabled?
Don't worry, you simply got trapped by a very unusual constant definition^^. Just try:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);
For whatever reasons Varien decided to define this STATUS_DISABLED constant with a value of 2, instead of the more intuitive (and commonly used) value of 0.
I think you can do this by setting store to default like
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
make sure you save the current store value and set it back after doing the above.
Or by using a script from outside magento and invoke mage by
require_once '../app/Mage.php';
$app = Mage::app();
Mage::register('isSecureArea', true);
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->setOrder('price', 'ASC');
I haven't found an answer as such to my question above. But I have saved a lot of time by using a different method.
I exported a CSV of all the products in my Magento, deleted all columns except Category ID and SKU (this is all I needed), and then filtered that to return all the skus instead.
If it helps anyone here is the code -
<?php
$file = fopen('allprods.csv', 'r');
// You can use an array to store your search ids, makes things more flexible.
// Supports any number of search ids.
$id = array($_GET['id']);
// Make the search ids safe to use in regex (escapes special characters)
$id = array_map('preg_quote', $id);
// The argument becomes '/id/i', which means 'id, case-insensitive'
$regex = '/'.implode('|', $id).'/i';
$skus = array();
while (($line = fgetcsv($file)) !== FALSE) {
list($ids, $sku) = $line;
if(preg_match($regex, $ids)) {
$skus[] = $sku;
}
}
$count = count($skus);
$i = 1;
echo $category_id;
foreach ($skus as $sku){
echo $sku;
if($i != $count) { echo "`"; }
$i++;
}
This solution was created by using a previous topic about filtering CSVs, here
So for now, I can survive. however - an answer to this question is still needed!
Nothing works if catalog_flat_product is on in backend configuration->catalog.
try this..this will give all the products enabled and disabled ultimately
$collection = Mage::getResourceModel('catalog/product_collection'); //this will give you all products
foreach($collection as $col)
{
$var = Mage::getModel('catalog/product')->loadByAttribute('sku',$col->getSku());
echo"<pre>";print_r($var->getData());echo"</pre>";
}
its really simple and after this you can easily filter products by status

$item->getProduct()->getQty not working in magento

I wrote a function in shipping.php. I want to get the quantity from each product in the cart. I am using $item->getProduct()->getQty, but this is not working.
Is there any other method?
Have you tried $item->getQty() ?
For future readers: look into the relevant source code, e.g app/code/core/Mage/Sales/Model/Order/ to find the exact API.
In that case, the code you are looking for is probably $item->getQtyOrdered(). Not sure which one you're referring to above.
$cart = Mage::getModel('checkout/cart')->getQuote();
$result = array();
$i = 0;
foreach ($cart->getAllItems() as $item) {
$result[$i]['id'] = $item->getProduct()->getId();
$result[$i]['name'] = $item->getName();
$result[$i]['sku'] = $item->getSku();
$result[$i]['price'] = $item->getPrice();
$result[$i]['qty'] = $item->getQty();
$i++;
}
echo "<pre>";
print_r($result);
Try this.
You can use
$item->getId();
to get the cart product ID too.
$item->getProduct()->getQty; will be returned by NULL;

Resources