Get product attribute in cart in Magento2

There is no necessity to change any PHP code for doing this.

You just need to create {MODULE_NAME}/etc/catalog_attributes.xml with such content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="sample_attr"/>
    </group>
</config>

So I found a way to get the information I was after. I have to load the complete product from the ProductRepository! Note that if you try and load it from \Magento\Catalog\Model\Product it will behave like a singleton, always giving you the same product in for each loops.

I used my modules helper class as it defines the following method to load the product from the productId:

public function __construct(
  \Magento\Framework\App\Helper\Context $context,
  \Magento\Catalog\Model\ProductRepository $productRepo
) {
  $this->_productRepo = $productRepo;
  parent::__construct($context);
  }


/**
 * Load product from productId
 *
 * @param $id
 * @return $this
 */
public function getProductById($id)
{
    return $this->_productRepo
        ->getById($id);
}

I included the helper in the template:

$customHelper = $this->helper('MyCompany\MyModule\Helper\Data');

I load the full product:

$custProd = $customHelper->getProductById($product->getId());

And now I can use:

$custProd->getSampleAttr();

and

$custProd->getData('sample_attr');

to get the data.


For those landed here looking for a simple solution, that's how it worked out for me

I added to my default.phtml (in module-checkout/view/frontend/templates/cart/item) this:

<?php
    $product_id = $product->getId();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customProduct = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
 ?>

then you can call $customProduct->getData('your_attribute'); as always