7

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?

10

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');
| improve this answer | |
5

In Magento 2

Change the 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
{
   protected $_objectManager;

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\ObjectManagerInterface $interface,
        \Magento\Quote\Model\Quote\Item $quote,
        \CollectionFactory $productCollectionFactory
    ) {
        $this->_objectManager = $objectManager;
        $this->cart = $cart;
        $this->product = $product;
        $this->objectManager = $interface;
        $this->quote = $quote;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();
        $quoteItem = $observer->getQuoteItem();
        $quoteItem->setCustomAttribute($product->getCustomAttribute());
    }
}

etc/events.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">

            <event name="sales_quote_item_set_product">
                <observer
         name="product_point_quote"
         instance="Namespace\Modulename\Observer\salesQuoteItemSetCustomAttribute"/>
            </event>
</config>

Create catalog_attributes.xml in etc to convert attribute to quote:

<?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>

This will bring your attribute on Cart Page

| improve this answer | |
  • 1
    It is also possible by the way to add almost any custom data you need in QuoteItem object by using: $quoteItem->setData('categoryData', $categoryData); - in observer execute() method. For this you don't need to add anything in catalog_attributes.xml – Bartosz Kubicki Oct 27 '16 at 11:37

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.