0

I'm trying to call a custom attribute in the cart.phtml and success.phtml pages so that a module can see the text that is contained. The attribute is a dropdown that isn't visible on the frontend at all.

The attribute contains a keyword that the module searches for, if the keyword is found, different finance options appear.

I've got this working on the product page by adding the following to view.phtml and it works perfectly but using the same/similar code on cart.phtml and success.phtml does not work:

$pro->getAttributeText('my_attribute')

I've done some searches and found that I needed to create a custom module and add the attribute in the config.xml of this module but that didn't work. My php knowledge isn't great so I'm sure I'm missing something.

This is what the snippet of code looks like from cart.phtml:

$prodname = "";

if($cart->getAllItems()):
    foreach ($cart->getAllItems() as $item):
        $prodname .= "|||".$item->getProduct()->getAttributeText('my_attribute').$item->getProduct()->getName();
    endforeach;
endif;

I've also tried with $_item and without the ->getProduct() with no luck.

In the admin panel I can see my custom module where I've put the config.xml is enabled. This is the config.xml:

<config>
<modules>
<module_name>
<version>0.0.1</version>
</module_name>
</modules>
    <global>
        <sales>
            <quote>
                <item>
                    <product_attributes>
                        <my_attribute />
                    </product_attributes>
                </item>
            </quote>
        </sales>
    </global>
</config>

Help me stackexchange, you're my only hope

0

If you are not getting your custom attribute value/text, then you can try to load the product using id that you will get from $item->getProduct()->getData(). I think $item->getProduct() is not loading all data that is why you are getting your custom product attribute data.

0

In your foreach loop, try:

$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());

This however will have impact on your site performance. See https://stackoverflow.com/questions/19379792/magento-get-a-custom-attribute-value-without-loading-the-entire-product for possible improvement.

Alternatively you can implement a custom collection load with only the attribute you want to prevent too many call to the db.

  • Thanks for the suggestion but that didn't work. I tried the suggestion in the other thread and had no luck also. – sMiLEy sLOth Feb 19 '18 at 11:13

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.