2

In the checkout-cart (template/checkout/cart/item/default.phtml) I have this code:

$simpleProduct = $this->getItem()->getOptionByCode('simple_product')->getProduct();

I want to use the same code in the Order Mail (template/email/order/items/order/default.phtml)

<?php $_item = $this->getItem() ?>
...
$simpleProduct = $_item->getOptionByCode('simple_product')->getProduct();

This leads to this error:

PHP Fatal error:  Call to a member function getProduct() on a non-object

So how can I get the simple_product of configurable product in mail order?

2

nbar, Please use sku field of order item. In Magento if you ordered a configurable product then Magento is save configurable product's child simple product sku has saved in DB.

You need to load simple product by this sku

$simplePro=Mage::getModel('catalog/product')->loadbyAttribute('sku',$_item->getSku())
1

I had this exact issue. You need to load a quote_item object which is where that method (getOptionByCode()) is defined.

Here's one way of doing this; probably not the most efficient, but it works:

<?php 

// template/email/order/items/order/default.phtml

$_item    = $this->getItem();
$_order   = $this->getItem()->getOrder();

// Get quote data
$quoteId      = $_order->getQuoteId();
$quote        = Mage::getModel('sales/quote')->load($quoteId);
$quoteItemId  = $_item->getQuoteItemId();

// Load quote items
$quoteItemCollection = Mage::getResourceModel('sales/quote_item_collection');
$quoteItemCollection->setQuote($quote);

// Loop through quote items and identify the current one
// You can maybe do this above somehow using a filter method
foreach ($quoteItemCollection->getItems() as $quoteItem) {

    // Current item is the target (top-level scope) item?
    if ($quoteItem->getId() == $quoteItemId) {

        $itemProduct = $quoteItem->getProduct();

        // Here's the getOptionByCode line
        if ($itemProduct->isConfigurable() && $quoteItem->getOptionByCode('simple_product')) {
            // Party
        }
    }
}
1
  • +1 for // Party - Good answer, I had to customize this slightly for where I'm using it. – NotJay Dec 4 '15 at 15:23
0
$product = Mage::getModel('catalog/product')->load($item->getProductId());
$configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
$simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($simpleCollection as $simpleProduct){
    echo "name:".$simpleProduct->getName();
    echo "<br>";
    echo "price:".$simpleProduct->getPrice();
    echo "<br>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.