1

I need to get a custom attribute value to Min cart, But I am missing some thing on the catalog_attributes.xml to get the attribute value.

di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\CustomerData\AbstractItem">
        <plugin name="Tech_mincart_Product_attribute" type="Tech\ConfigProductDynamicName\Plugin\Checkout\CustomerData\DefaultItem"/>
    </type>
</config>

catalog_attributes.xml

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

DefaultItem.php

<?php

namespace Tech\ConfigProductDynamicName\Plugin\Checkout\CustomerData;

class DefaultItem
{

    public function aroundGetItemData(
            \Magento\Checkout\CustomerData\AbstractItem $subject,
            \Closure $proceed,
            \Magento\Quote\Model\Quote\Item $item
    ) {
        $data = $proceed($item);
        $result['pcb_master'] = $item->getProduct()->getAttributeText('pcb_master');


        return \array_merge($result,$data);
    }


}

View/frontend/layout/checkout_cart_sidebar_item_renderers.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="minicart">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="minicart_content" xsi:type="array">
                            <item name="config" xsi:type="array">
                                <item name="itemRenderer" xsi:type="array">
                                    <item name="default" xsi:type="string">defaultRenderer</item>
                                    <item name="simple" xsi:type="string">defaultRenderer</item>
                                    <item name="virtual" xsi:type="string">defaultRenderer</item>
                                </item>
                            </item>
                            <item name="children" xsi:type="array">
                                <item name="item.renderer" xsi:type="array">
                                    <item name="component" xsi:type="string">uiComponent</item>
                                    <item name="config" xsi:type="array">
                                        <item name="displayArea" xsi:type="string">defaultRenderer</item>
                                        <item name="template" xsi:type="string">Tech_ConfigProductDynamicName/minicart/item/default</item>
                                    </item>
                                    <item name="children" xsi:type="array">
                                        <item name="item.image" xsi:type="array">
                                            <item name="component" xsi:type="string">Magento_Catalog/js/view/image</item>
                                            <item name="config" xsi:type="array">
                                                <item name="template" xsi:type="string">Magento_Catalog/product/image</item>
                                                <item name="displayArea" xsi:type="string">itemImage</item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Template/minicart/item/default.html

<div class="product-item-details" >
    <strong class="product-item-name" >
    <!-- ko if: product_has_url -->
    <a data-bind="attr: {href: product_url}, text: product_name"></a>
    <p data-bind="attr: {href: product_url}, text: product_name"> </p>
     <input data-bind="value: pcb_master"
            type="text"
            class="pcb_master" />
    <!-- /ko -->
    <!-- ko ifnot: product_has_url -->
     <!-- ko text: product_name --<!-- /ko -- >
    <!-- /ko -- >
    </strong>

I am not getting the custom attribute pcb_master in DefaultItem.php at

$result['pcb_master'] = $item->getProduct()->getAttributeText('pcb_master'); 
1

I am not sure about the code

$result['pcb_master'] = $item->getProduct()->getAttributeText('pcb_master');

Please do some changes

  • Do an indexing and cache flush.
  • $result['pcb_master'] = $item->getProduct()->getData('pcb_master');
0

I got two Solution to achieve this.

First Solution:

Use getData('pcb_master') instard of getAttributeText('pcb_master')

From

$result['pcb_master'] = $item->getProduct()->getAttributeText('pcb_master');

To

$result['pcb_master'] = $item->getProduct()->getData('pcb_master');

Second Solution:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());
$result['pcb_master'] = $_product->getResource()->getAttribute('pcb_master')->getFrontend()->getValue($_product);

I am using the First one, I am not sure the second one is the right way, But it works.

I Referred this blog https://www.magevision.com/blog/post/get-a-product-attribute-in-mini-cart-magento-2/

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.