5

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 ?

2
  • try the $item->getData('dp_width'); Commented Jun 18, 2015 at 9:42
  • What error are you getting?
    – Mufaddal
    Commented Jun 18, 2015 at 9:48

5 Answers 5

27

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');
8
  • And then i could get the attribute as $item->getAttributeText('dp_width');
    – ntzz
    Commented Jun 18, 2015 at 9:57
  • Yes, exactly, this is the correct variant!
    – Neklo.com
    Commented Jun 18, 2015 at 10:04
  • This is actual process +1 vote
    – Amit Bera
    Commented Jun 18, 2015 at 10:10
  • and for getting try $item->getProduct()->getAttributeText('dp_width');
    – Amit Bera
    Commented Jun 18, 2015 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
    Commented Jun 18, 2015 at 10:34
4

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

1
  • Dont run, i think it´s because this values are not printed on frontend, so it returns 0.
    – ntzz
    Commented Jun 18, 2015 at 10:52
2
$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')
1
  • 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
    Commented Jun 18, 2015 at 9:46
0

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

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

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 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.