Magento 2 – Set Custom Attribute Value on Cart Page

cartcustommagento-1.9magento2product-attribute

In Magento 1.x

I have created following function in Namespace/Modulename/Model/Observer.php

public function salesQuoteItemSetCustomAttribute($observer){

        $quoteItem = $observer->getQuoteItem();
        $product = $observer->getProduct();
        $quoteItem->setCustomerProductPoints($product->getCustomerProductPoints());
    }

Called this function in Namespace/Modulename/etc/config.xml

    <sales_quote_item_set_product>
                <observers>
                    <product_point_quote>
                        <class>productpoint/observer</class>
                        <method>salesQuoteItemSetCustomAttribute</method>
                    </product_point_quote>
                </observers>
    </sales_quote_item_set_product>

Also Have converted my custom attribute quote to order and from order to quote by using following code in config.xml

    <sales_convert_quote_item>
        <customer_product_points>
            <to_order_item>*</to_order_item>
            <to_invoice_item>*</to_invoice_item>
            <to_shipment_item>*</to_shipment_item>
            <to_cm_item>*</to_cm_item>
        </customer_product_points>
    </sales_convert_quote_item>
    <sales_convert_order_item>
        <customer_product_points>
            <to_quote_item>*</to_quote_item>
            <to_invoice_item>*</to_invoice_item>
            <to_shipment_item>*</to_shipment_item>
            <to_cm_item>*</to_cm_item>
        </customer_product_points>
    </sales_convert_order_item>

In Magento 2

I tried doing the same thing in Magento 2 but not able to get details of product.

I tried with following code:

Namespace/Modulename/Observer/salesQuoteItemSetCustomAttribute.php

<?php
namespace Namespace\Modulename\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart;


class salesQuoteItemSetCustomAttribute implements ObserverInterface
{
    public function __construct(
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\Product $product
    ) {
        $this->cart = $cart;
        $this->product = $product;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $cartQuote= $this->cart->getItems()->getData();
        $prod= $this->product->getData();
        echo '<pre>';print_r($prod); exit;
    }
}

etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_save_after">
        <observer name="set_checkout_quote_id" instance="Magento\Checkout\Observer\SalesQuoteSaveAfterObserver" />
    </event>
</config>

etc/fieldset.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../../../../../lib/internal/Magento/Framework/Object/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_quote_item">
            <field name="product_point">
                <aspect name="to_order_item" />
                <aspect name="to_invoice_item" />
                <aspect name="to_shipment_item" />
                <aspect name="to_cm_item" />
            </field>
        </fieldset>
        <fieldset id="sales_convert_order_item">
            <field name="product_point">
                <aspect name="to_quote_item" />
                <aspect name="to_invoice_item" />
                <aspect name="to_shipment_item" />
                <aspect name="to_cm_item" />
            </field>
        </fieldset>
    </scope>
</config>

Using the above code I am getting values of attributes in cart but not getting my custom attribute value. When I try to print Product Data it returns null.

The code works well with Magento 1.9. How do I make it work with Magento 2.0.2?

How to set custom attribute and display it cart page in Magento 2?

Best Answer

No need to create any Observe, you can get it by simple below code.

Just need to create {MODULE_NAME}/etc/catalog_attributes.xml with below 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="custom_attribute"/>
    </group>
</config>

You can get custome attribute in /templates/cart/item/default.phtml with below code.

echo $_item->getProduct()->getData('custom_attribute');