1

There do exist many questions about product attributes in order items. I hope my question has a new aspect.

If I have an order item I can use Mage_Sales_Model_Order_Item::getProduct to get the product:

public function getProduct()
{
    if (!$this->getData('product')) {
        $product = Mage::getModel('catalog/product')->load($this->getProductId());
        $this->setProduct($product);
    }

    return $this->getData('product');
}

When we use Mage::getModel('catalog/product')->load($id) we have access to all product attributes and since the getProduct() method makes use of it I was quite surprised to see that I did not get all product attributes from this:

     foreach( $order->getAllItems() as $item ){
        $product = $item->getProduct();
        Mage::log($product->debug()); 
     }

My question is: Which part is responsible for setting the product data to an order item?

And how can I extend it? My goal is to get the value of a custom boolean product attribute from an order in an observer after the order has been placed.

In the meantime I found the answer I was looking for (see answer below). But if someone can explain to me why it is not sufficient to clear the cache but instead rebuild the index as well, I will gladly accept this as the answer. It seems the info from the config.xml are written to an index table. Which table and what process is this?

3
  • Are you using flat product and does it work if you disable it?
    – user4351
    Nov 8, 2014 at 11:09
  • No, look at my answer. But thanks :) Nov 8, 2014 at 11:58
  • Ah crap, now that I see it, I've fought that battle before.
    – user4351
    Nov 8, 2014 at 12:06

2 Answers 2

2

An order should be a "snapshot" of the items status at the moment the order was placed and should NOT change as when that product attribute is updated.

Therefore a better way to accomplish this would to to add this field to your order item table.

See product attribute to quote item and order item and Adding custom product attribute to quote and order items

1
  • Yes, you are correct principally. Product attributes should be fixed but in this case I want an info to be shown after the order was placed only once. An extra field in the database feels like a "too-big-solution". Anyways. I found the solution for my initial question. But thanks anyway :-) Nov 8, 2014 at 11:30
2

I just found the answer to my question. It can be done using config.xml

<global>
   <sales>
        <quote>
            <item>
                <product_attributes>
                    <my_custom_attribute/>
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

After adding this, not only the cache needs to be refreshed but the index needs to be rebuild, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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