Hello, Magento Friends!
Magento Custom Development is a must-have for any business to fill the needs and save time and efforts to serve an excellent shopping experience to the shoppers. With the aid of smooth modifications and amalgamations in the frontend as well as backend, the Magento 2 E-commerce can provide a first-class user experience where it allows the user to set the custom price while adding a product to the cart. We are making headway to appending a short fragment of code that can outweigh all the prices of products with your intended one.
Introduction
Often, it happens that the store owner is willing to set a fixed product price even if multiple options/add-ons are selected by the customer from the front end for all or some specific products. At that time we need to manually code to override all store product prices with your desired custom price instead of manually editing all store products. Also, have a glance at the Magento 2 Custom Price Extension by MageComp which enables the admin to set a product price and the customer to add their desired product price.
In this tutorial blog, we will be demonstrating how you can Add Product to Cart with the Custom Price in Magento 2. This action triggers the already calculated price by Magento and lets you add your customized price.
Steps to Add Product To Cart With Custom Price in Magento 2
Step 1: First, create a file “events.xml” at the below-given path.
app\code\Vendor\Extension\etc\frontend\events.xml
Now add the below code
<?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="customprice" instance="Vendor\Extension\Observer\Customprice" /> </event> </config>
Step 2: Now, we need one more file named “Customprice.php” that overrides our price. Go to the below path
app\code\Vendor\Extension\Observer\Customprice.php
And finally add the code as mentioned below,
<?php namespace Vendor\Extension\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; //set your price here $item->setCustomPrice($price); $item->setOriginalCustomPrice($price); $item->getProduct()->setIsSuperMode(true); } }
That’s it!
Bottom Line:
And, bingo! Hope everyone is now aware of How to Add Product To Cart With Custom Price in Magento 2. You are free to play and manipulate this according to your need for setting a custom price for one or more products by adding conditions. Also, get the Magento Custom Extension Development Service and custom develop your Magento store as per your business needs.
Let us know by commenting below if you are facing any issues during the implementation of this code.