Magento 2 – Programmatically Update Product Price and Add to Cart

catalogmagento2.3PHPprogrammatically

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

Best Answer

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 :)

Related Topic