1

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;
            $_product->setPrice(0);
            $this->_cart->addProduct($_product,$params);
            $this->_cart->save();

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

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Dhaval Vaghela
  • 1,102
  • 16
  • 35

2 Answers2

1

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" />
    </event>
</config>

and

<?php

    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)
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }

    }

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

inrsaurabh
  • 1,676
  • 2
  • 26
  • 54
  • i want to add one additional product and update price of that product. – Dhaval Vaghela Mar 22 '18 at 06:30
  • Let me try to merge your code with my condition. – Dhaval Vaghela Mar 22 '18 at 06: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 pointcheckout_cart_product_add_after` from where we can change the cart values – inrsaurabh Mar 22 '18 at 06: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 ? – Dhaval Vaghela Mar 22 '18 at 06: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 06:55
0

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

hungersoft
  • 447
  • 3
  • 4