I have developed a custom module to show additional product info on cart page. My module is combo of:
- https://webkul.com/blog/add-product-information-checkout-cart-in-magento2/ (https://github.com/webkul/magento2_cartPageAdditionalInfo) &
- https://www.atwix.com/magento-2/how-to-add-the-details-for-cart-item-in-magento-2/
I want to fetch the current product attribute as additional info for every next product in cart. But I get the same product attribute/name which is first cart product.
Basically I have extended module 1 and added module 2 into it. So instead of returning static text Additional information from module 1 block method. I wanted to return custom attribute of current product using module 2.
See following image:
My block source code:
namespace Mcs\AccessoryMainData\Block;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Block\Cart\Additional\Info as AdditionalBlockInfo;
use Magento\Framework\Exception\LocalizedException;
/**
* AdditionalProInfo.
*/
class AdditionalProInfo extends \Magento\Framework\View\Element\Template
{
/**
* Product
*
* @var ProductInterface|null
*/
protected $product = null;
/**
* Product Factory
*
* @var ProductInterfaceFactory
*/
protected $productFactory;
protected $abstractViewBlock;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
ProductInterfaceFactory $productFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Registry $registry,
\Psr\Log\LoggerInterface $logger,
\Magento\Catalog\Block\Product\View\AbstractView $abstractViewBlock,
\Magento\Catalog\Block\Product\AbstractProduct $abstractProduct,
array $data = []
) {
parent::__construct($context, $data);
$this->productFactory = $productFactory;
$this->checkoutSession = $checkoutSession;
$this->registry = $registry;
$this->logger = $logger;
$this->abstractViewBlock = $abstractViewBlock;
$this->abstractProduct = $abstractProduct;
}
/**
* @return void
*/
protected function _construct()
{
parent::_construct();
}
/**
* @return $this
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
}
/**
* @return additional information data
*/
public function getAdditionalData()
{
$product = $this->getProduct();
return $product->getName();
}
/**
* Get product from quote item
*
* @return ProductInterface
*/
public function getProduct()
{
if ($this->product instanceof ProductInterface) {
return $this->product;
}
try {
$layout = $this->getLayout();
} catch (LocalizedException $e) {
$this->product = $this->productFactory->create();
return $this->product;
}
/** @var AdditionalBlockInfo $block */
$block = $layout->getBlock('additional.product.info');
if ($block instanceof AdditionalBlockInfo) {
$item = $block->getItem();
$this->product = $item->getProduct();
}
return $this->product;
}
}
Following is my layout xml file code:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="additional.product.info">
<block class="Mcs\AccessoryMainData\Block\AdditionalProInfo"
name="cart_item_addional_info_block"
template="Mcs_AccessoryMainData::checkout/cart/item/additionalinfo.phtml"
cacheable="false"
/>
</referenceContainer>
</body>
</page>
Following is my phtml file source code:
$_item = $block->getItem();
$product = $_item->getProduct(); // Get cart product details
$additional_data = $block->getAdditionalData(); // Get cart product additionl details defined in block page
?>
<div>
<span><?php echo $additional_data?></span>
</div>
Please help me on this.