0

Can i use any event for update my cart product price while reload cart and checkout page ?

any idea please share..

  • Hi, Can you share criteria on why you need to update the cart product price with a page load? – Haritha Mar 22 '18 at 10:03
  • i need to add one custom product and set custom price. currently i used checkout_cart_product_add_after this event for that but it will revert back product price on checkout page. – Camit1dk Mar 22 '18 at 10:05
3

We can set the custom pricing while adding a product to cart or edit the product from the shopping cart.

Hence I have used two observers to update the custom pricing for the product. You can update the logic accordingly. Hope you have already created the module.

1) Set the custom pricing while adding a product to cart.

<?php

namespace VendorName\Changeprice\Observer;

use Magento\Framework\Event\ObserverInterface;

/**
 * Class Cart
 * @package VendorName\Changeprice\Observer
 */
class Cart implements ObserverInterface
{
    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {

        $item = $observer->getEvent()->getData('quote_item');

        $product = $observer->getEvent()->getData('product');

        $price ='';

        $cartItems = [];
        if($item->getQuote()->getItems()){
            foreach ($item->getQuote()->getItems() as $key => $value) {
                $cartItems[$value->getSku()] = $value->getQty();
            }
        }
        // add your logic for custom price
        $item->setOriginalCustomPrice($price);
        $item->setCustomPrice($price);
    }
}

2) Updating the price while the product has been edited from the shopping cart.

<?php

namespace VendorName\Changeprice\Observer;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Event\ObserverInterface;

/**
 * Class Updatecart
 * @package VendorName\Changeprice\Observer
 */
class Updatecart implements ObserverInterface
{
    /**
     * @var CheckoutSession
     */
    protected $_checkoutSession;

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

    /**
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $data = $observer->getEvent()->getData('info');

        $cart = $observer->getEvent()->getData('cart');
        $price ='';

        $convert_data = (array)$data;

        foreach ($convert_data as $itemsdata=>$datainfo) {
            foreach ($datainfo as $itemId => $itemInfo) {
                $item = $this->_checkoutSession->getQuote()->getItemById($itemId);

                if (!$item) {
                    continue;
                }
                // add your logic for custom price
                $item->setOriginalCustomPrice($price);
                $item->setCustomPrice($price);
            }
        }
    }
}

3) Declaring Observers for events

<?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="checkout_cart_product_add_after">
        <observer name="vendorname_checkout_cart_product_add_after" instance="VendorName\Changeprice\Observer\Cart"/>
    </event>
    <event name="checkout_cart_update_items_after">
        <observer name="vendorname_checkout_cart_update_items_after" instance="VendorName\Changeprice\Observer\Updatecart"/>
    </event>
</config>

I hope this might help!

| improve this answer | |
  • 1
    +50 for your lines – Sanjun Dev Sep 17 '19 at 6:12

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.