I was trying to define some exceptions, and one of them is about the volume of the product. I've got width-length-height attributes in the products. And i was trying to get the total of these 3 attributes of all the products at my cart.

So i was trying things like this:

$cart = Mage::getModel('checkout/cart')->getQuote();
        $totalV= 0;

        foreach ($cart->getAllItems() as $item) {

        $prod_W = $item->getAttributeText('dp_width');
        $prod_H = $item->getAttributeText('dp_height');
        $prod_L = $item->getAttributeText('dp_length');
        $totalV += $prod_H + $prod_L +$prod_W;
        }

        ?>


        <input type = "hidden" class = "onestepcheckout-vol-prod" value = "<?php echo $totalV;?>
               " >
        </input>

I tries to using $item->getProduct()->getAttributeText('X'); but it gives me an error.

How could i get these values ?

  • try the $item->getData('dp_width'); – saravanavelu Jun 18 '15 at 9:42
  • What error are you getting? – Mufaddal Jun 18 '15 at 9:48
up vote 25 down vote accepted

Load product in foreach is a bad idea, though working.

Better way:

in config.xml

<global>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <dp_width/>
                    <dp_height/>
                    <dp_length/>
                </product_attributes>
            </item>
        </quote>
    </sales>
</global>

And...

$item->getProduct()->getAttributeText('dp_width');
  • And then i could get the attribute as $item->getAttributeText('dp_width'); – ntzz Jun 18 '15 at 9:57
  • Yes, exactly, this is the correct variant! – Neklo.com Jun 18 '15 at 10:04
  • This is actual process +1 vote – Amit Bera Jun 18 '15 at 10:10
  • and for getting try $item->getProduct()->getAttributeText('dp_width'); – Amit Bera Jun 18 '15 at 10:11
  • Before making it, and cleaning cache, i make a try and gives me this error: Call to a member function getSource() on a non-object in /home/XXX/public_html/app/code/core/Mage/Catalog/Model/Product.php – ntzz Jun 18 '15 at 10:34

you can try this:

$item->getProduct()->getResource()->getAttribute('dp_width')->getFrontend()->getValue($item->getProduct());

$item->getProduct()->getResource()->getAttribute('dp_length')->getFrontend()->getValue($item->getProduct());

$item->getProduct()->getResource()->getAttribute('dp_height')->getFrontend()->getValue($item->getProduct());

I have Using product Resource model

  • Dont run, i think it´s because this values are not printed on frontend, so it returns 0. – ntzz Jun 18 '15 at 10:52
$item->getProduct()->getAttributeText('X') 

$item donot have all the attributes information in quote, so the above code will not work.

instead you can try the below code

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

$product->getAttributeText('X')
  • As you say, i try like you, But at the end, it gives me the same error as i say at final: Call to a member function getSource() on a non-object – ntzz Jun 18 '15 at 9:46

Load Product in foreach

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

you can able to get Product attributes like this

  • The same error as i say to the other mate down Call to a member function getSource() on a non-object – ntzz Jun 18 '15 at 9:48
  • 'Show in Product Listing' needed to be set to yes in the attribute editor in admin – Mufaddal Jun 18 '15 at 9:54
  • cannot do this . – ntzz Jun 18 '15 at 10:13

Try this

<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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