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
}
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
  • you sure it could works on "catalog_product_collection_load_after" event? – Edwin Widhiyanto Sep 13 '17 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 Sep 13 '17 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 – Edwin Widhiyanto Sep 13 '17 at 10:26

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.