Magento – Magento2: How to get simple product color and size

magento2product

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

Best Answer

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();