3

I tried,

 $items = $item->getQuote()->getAllItems();

 foreach($items as $item1)
 {
      $productId = $item1->getProductId();
      $itemId = $item1->getItemId();
 }

I am getting blank value.

Can anyone tell me how to call checkout_cart_add_product_complete event in observer ?

2

For call observer checkout_cart_add_product_complete you need to create a module and create event.xml file in frontend area

etc/frontend/event.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_add_product_complete">
         <observer name="test_callobserver" instance="Test\Callobserver\Observer\Cartitems"/>
    </event>
</config>

2) Now call Cartitems in path Test\Callobserver\Observer\

<?php
namespace Test\Callobserver\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;

use \Magento\Checkout\Model\Session as CheckoutSession;

class Cartitems implements ObserverInterface {

    /** @var CheckoutSession */
    protected $checkoutSession;

    /**
     * @param CheckoutSession $checkoutSession
     */
    public function __construct(CheckoutSession $checkoutSession) {
        $this->checkoutSession = $checkoutSession;
    }

    public function execute(Observer $observer) {
        /** @var \Magento\Catalog\Model\Product\Interceptor $product */
        $product = $observer->getProduct();

        /** @var \Magento\Quote\Model\Quote  */
        $quote = $this->checkoutSession->getQuote(); //here you will get quote data
    }
}

This code help you.

| improve this answer | |
  • How to update custom attribute in given quote item table – jassi Sep 12 '17 at 5:57
  • 1
    First You need to find $quoteItem from above code then simply set custom attribute with code $quoteItem->setCustomAttribute($product->getCustomAttribute()); – akgola Sep 12 '17 at 7:19

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.