0

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.

1

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

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

<?php

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

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.

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

        }

    }

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.