0

I tried to update the price of each product in cart each time I load a page. I used observer and event catalog_product_collection_load_after. I tried to get each item in the cart from observer but its always null if I do var_dump for it. Am I doing it wrong?

Observer :

public function execute(\Magento\Framework\Event\Observer $observer) {
     $quoteItem = $observer->getQuoteItem();
     var_dump($quoteItem); //always NULL
}

2 Answers 2

0

That event observer stores only collection data:

$collection = $observer->getEvent()->getCollection();
foreach ($collection as $product) {
...
}
0
Try Code:

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


    Reference Link:
    https://magento.stackexchange.com/a/192846/24878
3
  • you sure it could works on "catalog_product_collection_load_after" event? Commented Sep 13, 2017 at 9:32
  • if you want change cart item attribute then why use catalog_product_collection_load_after ?,you should try as per link.
    – akgola
    Commented Sep 13, 2017 at 9:54
  • i want to update price of each product in cart each time page is loaded, because i tried use on fly data, so if in my data the price updated, i should update the price on the cart too Commented Sep 13, 2017 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.