0

I create one Module and make a custom form which is display in the catalog product view page. when the customer change option from dropdown then the product price will change and also that updated price goto cart.

enter image description here

1

Try this,

Use checkout_cart_product_add_after

Create a module and add events.xml in following path

app/code/Vendor/Module/etc/events.xml

Add the below code in it

<?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="setprice" instance="Vendor\ModuleName\Observer\SetPrice" />                                                                         
  </event>                                                            
</config>

then add observer in the below path

Vendor\ModuleName\Observer\SetPrice.php

add below code in it

<?php                                                                 
namespace Vendor\ModuleName\Observer;                                     
class SetPrice implements \Magento\Framework\Event\ObserverInterface          
{
 protected $_request;
 public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    array $data = []
 ) {
    $this->_request = $request;
 }

 public function execute(
    \Magento\Framework\Event\Observer $observer
 ) {
    $postdata = $this->_request->getPost();
    $priceoption = $postdata['input_name']; // say it select input field which you have
    $item = $observer->getEvent()->getData('quote_item');
    $item = ($item->getParentItem() ? $item->getParentItem() : $item);
    $price = $priceoption; //set your price here
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);
    $item->getProduct()->setIsSuperMode(true);
 }}

$postdata['input_name']; this must be you select input name which you have in the form and in addition to above answer move your input fields to add to cart form fields.

To move your custom form inputs to add to cart form in below way.

Add the catalog_product_view.xml then add the below code in it

<?xml version="1.0"?>                                             
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">                                                  
 <body>                                                 
    <referenceBlock name="product.info">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Vendor_ModuleName::product/view/form.phtml</argument>
        </action>
    </referenceBlock>                                           
 </body>                                                        
</page>

then copy the form.phtml from vendor in order to override and move your input fields to there as per your requirements.

vendor/magento/module-catalog/view/frontend/templates/product/view/form.phtml

To

app/code/Vendor/ModuleName/view/frontend/templates/product/view/form.phtml

Hope this helps :)

  • 1
    it work when product added to cart but first i want to update price in selecton of my custom options and after pass to cart – Bhavesh Prajapati Mar 7 at 11:59
  • You want to update price in where in table or in PDP – Prathap Gunasekaran Mar 7 at 15:00
2

You could do that

At magento2 you can change quote item mean cart item price using below code:

$quoteItem = ( $quoteItem->getParentItem() ? $quoteItem->getParentItem() : $quoteItemitem );
$price = 150; //set your price here
$quoteItem->setCustomPrice($price);
$quoteItem->setOriginalCustomPrice($price);
$quoteItem->getProduct()->setIsSuperMode(true); 

Most of the case, people using checkout_cart_product_add_after for set custom price to quote item.

I am not clear about your custom fields inside your custom form.I suggested that move all field of the custom field to Magento own add to cart form. And then using $quoteItem->getBuyRequest() at observer like sales_quote_item_set_product, we can get all fields value which is posted during add to cart.

Suppose, you have field an input field name 'my_own_price` and you want the value entered on this field will be set as price of this cart item.

<?php
/**
 * User: Amit Bera
 * Email: dev.amitbera@gmail.com
 */

namespace {VendorName}\{ModuleName}\Observer;


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

class {Class} implements  ObserverInterface
{



    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $quoteItem = $observer->getEvent()->getItem();
        try{
            $buyRequest = $quoteItem->getBuyRequest();
            if (is_object($buyRequest)) {
                $buyRequestArray = $buyRequest->toArray();
                if (array_key_exists('my_own_price', $buyRequestArray)) {
                    $quoteItem = ( $quoteItem->getParentItem() ? $quoteItem->getParentItem() : $quoteItemitem );
                    $price = $buyRequestArray['my_own_price']; //set your price here
                    $quoteItem->setCustomPrice($price);
                    $quoteItem->setOriginalCustomPrice($price);
                    $quoteItem->getProduct()->setIsSuperMode(true); 
                }

            }
        }catch (\Exception $exception)
        {

        }

    }
}
  • 1
    can u give me more info about how can i update price of product in frontend when customer select the form option. in way of magento – Bhavesh Prajapati Mar 7 at 11:52
1

You Can achieve this using below code

Step 1 Create events.xml file in below path

app/code/Namespace/ModuleName/etc/events.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!-- Save data in cart -->
    <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="namespace_modulename_checkout_cart_product_add_after" instance="Namespace\Modulename\Observer\CheckoutCartAddObserver" />
        </event>
    </config>

Step 2 : Create Observer file in below path

app/code/Namespace/ModuleName/Observer/CheckoutCartAddObserver.php

<?php                                                                 
namespace Vendor\ModuleName\Observer;                                     
class CheckoutCartAddObserver implements \Magento\Framework\Event\ObserverInterface          
{
 protected $_request;
 public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    array $data = []
 ) {
    $this->_request = $request;
 }

 public function execute(
    \Magento\Framework\Event\Observer $observer
 ) {
    $postdata = $this->_request->getPost();
    $priceoption = $postdata['your_dropdown_field_name']; // Your Drop down field name goes here 
    $item = $observer->getEvent()->getData('quote_item');
    $item = ($item->getParentItem() ? $item->getParentItem() : $item);
    $price = $priceoption; //set your price here
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);
    $item->getProduct()->setIsSuperMode(true);
 }}

Tell Me if above code is not work....!

@Prathap Gunasekaran There is no need to copy the form.phtml from vendor

  • Work Like Charm...! – Bhavesh Prajapati Mar 8 at 6:50
  • @sanjay form.phtml move his inputs to that form. So it requires and hope you understand what am trying to say. – Prathap Gunasekaran Mar 8 at 7:21
  • @Prathap Gunasekaran I have append my custom form with add to cart form using jQuery so i don't need to copy the form.phtml from vendor – Bhavesh Prajapati Mar 8 at 7:27
  • @BhaveshPrajapati You've mentioned as custom form which is why I've given such option to override. – Prathap Gunasekaran Mar 8 at 7:30
  • because i want to append add to cart form and your way is proper but i have append my add to cart form using JQuery var element = $('#fields').detach(); var form_class = $('#form_class').detach(); $('.box-tocart').prepend(element); $('.box-tocart').prepend(form_class); – Bhavesh Prajapati Mar 8 at 7:36

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.