0

I'm editing app\design\frontend\Vendor\theme\Magento_Checkout\templates\cart\item\default.phtml

This is what I'm trying to achieve:

Product color and size

For configurable product, it shows color and size by default, but they doesn't show up for simple product. The .phtml which I'm editing already have $product:

$product = $_item->getProduct();

I tried this:

<br>data :<?php print_r($product->getData())?>
<br>color :<?php var_dump($product->getColor())?>
<br>options :<?php var_dump($product->getOptions())?>

But it doesnt seem to have color or size value in it. Here's the output:

enter image description here

  • Please share your full code which you have tried. – Rakesh Jesadiya May 31 '17 at 4:56
  • edited the question – zekkai May 31 '17 at 5:54
0

First you have to create helper class in your module

namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper 
{
     public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
        array $data = []
    ) {
        $this->_ProductAttributeRepository = $productAttributeRepository;
        parent::__construct($context,$data);
    }
    public function getsize()
    {
        $size = $this->_ProductAttributeRepository->get('size')->getOptions();
        return $size;

    }
public function getcolor()
    {
        $color = $this->_ProductAttributeRepository->get('color')->getOptions();
        return $color;

    }
}

Then You can use this function in phtml file by calling helper function

$helper=$this->helper('Vendor\Module\Helper\Data');
$size = $helper->getsize();
$color = $helper->getcolor();
  • Thanks, but how do I use this helper if I want to get color and size value from $product variable? – zekkai May 31 '17 at 6:02

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.