1

I have developed a custom module to show additional product info on cart page. My module is combo of:

  1. https://webkul.com/blog/add-product-information-checkout-cart-in-magento2/ (https://github.com/webkul/magento2_cartPageAdditionalInfo) &
  2. 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:

Shopping cart product data

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.

  • Show your code that you tried – Sohel Rana Jan 5 at 8:13
  • Thanks for your reply @SohelRana. I have added now all my source code related to it. Please check it now. – Ghulam Akbar Jan 6 at 9:25
0

The issue was with Mcs\AccessoryMainData\Block\AdditionalProInfo:getProduct().

Here is the latest code for it:

public function getProduct()
{
    $product = null;
    try {
        $layout = $this->getLayout();
    } catch (LocalizedException $e) {
        $product = $this->productFactory->create();
        return $product;
    }
    /** @var AdditionalBlockInfo $block */
    $block = $layout->getBlock('additional.product.info');
    if ($block instanceof AdditionalBlockInfo) {
        $item = $block->getItem();
        $product = $item->getProduct();
        $this->logger->log('Info', 'Item data : ', [$item, $this->product]);
    }
    return $product;
}

Plus I was missing catalog_attribute.xml inside etc/ folder. This is the code for it:

<?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="name"/>
</group>

| improve this answer | |

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.