3

I have a product with a custom attribute, which works fine almost everywhere I use it.

However, when I try to get this attribute's value during the catalog_product_get_final_price event, I get null returned. Also, when I print the product's data, this attribute is not shown.

Is there a logical reason for this? How can I retrieve the value of this attribute?


I suspect that because this event is fired during the create/update of a quote item, not the original product data is used, but a (partial) snapshot of it's data. If that is the case; how might I make my attribute available in this snapshot as well?


config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Module>
            <version>1.0.0</version>
        </My_Module>
    </modules>
    <global>
        <events>
            <catalog_product_get_final_price>
                <observers>
                    <my_observer>
                        <class>My_Module_Model_Observer</class>
                        <method>calculateItemPrice</method>
                    </my_observer>
                </observers>
            </catalog_product_get_final_price>
        </events>
    </global>
</config>

Observer

<?php
class My_Module_Model_Observer
{
    /**
     * 
     * @param  Varien_Event_Observer $observer
     * @return void
     */
    public function calculateItemPrice( Varien_Event_Observer $observer )
    {
        $event   = $observer->getEvent();
        $product = $event->getProduct();

        // folowing returns NULL
        $product->getMyCustromAttribute()
    }
}

UPDATE

When I debug the $observer object in the event observer, I get the following data;

Varien_Event_Observer (
    [_data:protected] => Array
        (
            [event] => Varien_Event(...)
            [product] => Mage_Catalog_Model_Product(...)
            [qty] => 1
        )
    [_hasDataChanges:protected] => TRUE
    [_origData:protected] => NULL
    [_idFieldName:protected] => NULL
    [_underscoreCache:static:protected] => Array(...)
    [_isDeleted:protected] => FALSE
    [_oldFieldsMap:protected] => Array()
    [_syncFieldsMap:protected] => Array()
)
3
  • Do you want the value of the quote item? Because there are no products in the cart... Commented May 27, 2014 at 13:17
  • Can you add your code to the question?
    – Marius
    Commented May 27, 2014 at 13:43
  • update; added some code
    – Maurice
    Commented May 27, 2014 at 14:01

1 Answer 1

3

Not sure about this but I thought to throw it in here, maybe I'm right, or maybe it gives you a nudge in the right direction.
I think your suspicions are right. Because this call is made on a product that comes from a quote item, not all the attributes are loaded.

The quote item loads only the attributes specified in the config node global/sales/quote/item/product_attributes.
Try adding your attribute code between those attributes.
For this create a custom module (or use one that you have) and add this inside the config.xml file inside the <config> tag

<global>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <my_custrom_attribute /> <!-- your attribute code goes here -->
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

Clear the cache and give it a go.

2
  • That was exactly it! Cheers!
    – Maurice
    Commented May 27, 2014 at 14:14
  • in magento2 it would be catalog_attributes.xml but with lot less code.
    – R T
    Commented Oct 7, 2022 at 15:40

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.