2

I'm trying to get custom product attribute value (for attribute code flr01) in catalog_product_save_after observer without loading the product but I'm gettin null value if I'm not loading the product before using following functions

$qty = $observer->getProduct()->getAttributeText('flr01');

or

$qty = $observer->getProduct()->getData('flr01');

While loading the product before using getData() method, I'm getting flr01 value

$productId = $observer->getProduct()->getId();
$product = Mage::getSingleton('catalog/product')->load($productId);
$qty = $product->getData('flr01');

I think loading the product again is not recommended from performance point of view (I should already have all product attributes available in $observer->getProduct() ).

1
  • 1
    have you try this: $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product); – Amit Bera Jul 22 '15 at 6:02
1

You can get custom attribute data using below code at observer

$observer->getEvent()->getProduct()->getData('flr01')

and for getting : text value if it dropdown.

    $observer->getEvent()->getProduct()->getResource()->getAttribute('flr01')
->getFrontend()->getValue‌​($observer->getEvent()->getProduct());
10

To retrieve a single attribute directly from the database, you can use

$flr01 = $product->getResource()->getAttributeRawValue(
    $product->getId(),
    'flr01',
    Mage::app()->getStore()
);

If you need multiple attributes, you can also pass an array of attribute codes instead of a single attribute code, then the result is an array in the form [attribute_code => attribute_value].

Example:

$attributes = $product->getResource()->getAttributeRawValue(
    $product->getId(),
    ['flr01', 'flr02', 'flr03'],
    Mage::app()->getStore()
);

// Now you have the values in:
$attributes['flr01'];
$attributes['flr02'];
$attributes['flr03'];

Note that this will return you the raw value, i.e. if the attribute is not a simple text field attribute, but uses a dropdown, multiselect or a custom backend model, the attribute->getFrontend() method described by @Amit Bera will give you the formatted value as it gets displayed.

2
  • Can you give one example of fetching multiple attributes using above method? say flr01, flr02,flr03. – amitshree Jul 22 '15 at 8:27
  • See example in updated answer – Fabian Schmengler Jul 22 '15 at 8:30

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.