I have a custom price in phtml which is shown in the product page.Now I want to send that price to checkout page on add to cart click in magento2 that custom price is coming in js variable and input text field name. If anyone knows reply to me.

2 Answers 2


I think you should use checkout_cart_product_add_after event for this, in this event you will get quote_item (added product into the cart by the customer).

So first you need to create events.xml , in this file you need to define your event

<event name="checkout_cart_product_add_after">
        <observer name="change_price_add_to_cart" instance="Example\Sample\Observer\ChangePrice" />

Now you need to create your Observer so you can change your price,


namespace Example\Sample\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ChangePrice implements ObserverInterface

    public function execute(Observer $observer)
        $item = $observer->getEvent()->getData('quote_item');
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        $customerPrice = 9.99; //do your magic here for custom price

Yes, you can set that custom price using Event. See how to use Events and observers checkout magento link https://devdocs.magento.com/guides/v2.3/extension-dev-guide/events-and-observers.html

Run an observer on the event on checkout_cart_product_add_after and set custom price which is post from form. Magento has store add to cart form posted fields value at Quote item object's buying request method $quoteItem->getBuyRequest().At your observer, you can get that custom price input field value easily than using quote item setCustomPrice() method you can set your custom price.

    namespace Devbera\CarPrice\Observer;

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

    class CustomPrice implements ObserverInterface
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $quoteItem = $observer->getEvent()->getData('quote_item');         
            $quoteItem = ( $quoteItem->getParentItem() ? $quoteItem->getParentItem() : $quoteItem );
        $buyRequest = $quoteItem->getBuyRequest();
            if (is_object($buyRequest)) {
                $buyRequestArray = $buyRequest->toArray();

                if (array_key_exists('custom_price_field', $buyRequestArray)) {
            $price = $buyRequestArray['custom_price_field'];


  • i want to do it with controller nor observer Commented Apr 18, 2019 at 9:37
  • just a few seconds you are faster Master Amit :)
    – Keyur Shah
    Commented Apr 18, 2019 at 9:39
  • 1
    You have to use observer
    – Amit Bera
    Commented Apr 18, 2019 at 9:41
  • Don't Call me as master :).I am a developer like you
    – Amit Bera
    Commented Apr 18, 2019 at 9:46
  • @AmitBera, please help on this issue, its similar issue like above, but quoteid not retrieving,magento.stackexchange.com/questions/274172/… Commented May 13, 2019 at 8:24

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.