I have used this event for add product with a custom price: checkout_cart_product_add_after

its working fine for cart page. but when I redirect to checkout page then my custom product price has replaced with the original price which is already set in admin.

is there any way to save this custom price for place order whole process?

i used below code for add product with custom price :

            $params = array(
                'product' => $productIds,
                'qty' => 1
            $_product = $this->_productRepository->getById($productIds);
            //echo "<pre/>"; print_r($_product->getData()); exit;

is there any event to save this custom price for end of order? and which one code I used for that?

  • i updated my answer , it will help you to achieve – inrsaurabh Mar 22 '18 at 6:16

Create an events.xml and hook to checkout_cart_product_add_after

NOTE: $item->getProduct()->setIsSuperMode(true) in order to make $item->setCustomPrice() and $item->setOriginalPrice() work.

<?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="modulename-customprice" instance="Companyname\Modulename\Observer\CustomPrice" />



    namespace Companyname\Modulename\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;

    class CustomPrice implements ObserverInterface
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $item = $observer->getEvent()->getData('quote_item');         
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $price = 100; //your custom price here , you can make it dynamic as well ( use config values)


checkout_cart_update_items_after have a look this event as well as it runs for every items in cart.

Must read :

1) Magento2 - How to add a product into cart programatically when checkout_cart_product_add_after is fired

2) Create custom events

  • i want to add one additional product and update price of that product. – Camit1dk Mar 22 '18 at 6:30
  • Let me try to merge your code with my condition. – Camit1dk Mar 22 '18 at 6:33
  • you can use the same execute function to create a product/or add a already created product and than update the total price,I didnt tried but it seems that it will surely work , because we have a point checkout_cart_product_add_after` from where we can change the cart values – inrsaurabh Mar 22 '18 at 6:36
  • any idea about which one event i need to use for add product in cart? and then after which event use for update this cart price ? – Camit1dk Mar 22 '18 at 6:45
  • you can try checkout_cart_product_add_after , but i never tried to do the same, no idea currently. I am even trying to find the perfect solution which are stable as well – inrsaurabh Mar 22 '18 at 6:55

Change $_product->setPrice(0); to $_product->setCustomPrice(0);

  • Its not working. – Camit1dk Mar 22 '18 at 6:16

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.