6

I'm looping through the items in cart and printing some values, but I cannot get the product's attribute values.

Here's how I'm doing it:

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

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

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

    if ($item->getParentItemId()) {
        continue;
    }

    $size = $_product->getAttributeText('size');
    $color = $_product->getAttributeText('color');
    $prod_url = $_product->getProductUrl(); // Is printing ok
}

Both $size and $color don't have any value. Any ideas why?

5
  • Are you able to get $_product object? Dec 29, 2017 at 16:30
  • I think so. Being able to get the product Url means I'm getting the $_product object, right?
    – PedroSimao
    Dec 29, 2017 at 16:37
  • Please try this, mat that help you Dec 29, 2017 at 16:43
  • Sorry but I'm not being able to get the attributes using the code on that answer because $options outputs null
    – PedroSimao
    Jan 8, 2018 at 16:27
  • normally you will get all attribute data on model load. Check if $_product->getData('size') returns raw value. getAttributeText() may be the Problem Feb 21, 2018 at 9:42

1 Answer 1

3

Try to use this code. Instead of Mage::getModel('checkout/session') use Mage::getModel('checkout/cart')

        $quote = Mage::getModel('checkout/cart')->getQuote();
        foreach ($quote->getAllItems() as $item) {
            $product= Mage::getModel('catalog/product')->load($item->getProductId());

            if ($item->getParentItemId()) {
                continue;
            }

            $size = $product->getAttributeText('size');
            $color = $product->getAttributeText('color');
            $productUrl = $product->getProductUrl(); // Is printing ok
        }

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.