Magento 2 is well-known as a functional platform for eCommerce. However, custom development is still a necessity to satisfy business demand. Within its provision, Magento supports a variety of integrated customization where you are capable of setting the custom price for products in the cart, for instance. Instead of manually editing each store product, the solution we’re heading at is implementing a short snippet of code to your module to override all store products’ prices with your desired one.

Magento pricing

Speaking of price, Magento has a list of prices including Special Price, Tier Price, Grouped Price,  Minimum price of composite products, the Price range of composite products and Manufacturer price (MSRP).

Those prices are represented as price types in the code. For example, Special Price is represented by the final price type in the code.

Magento regularly applies the minimum of all the configured prices to the final products. Hence, the idea now is to set a fixed price for all products no matter how multiple options are selected.

Custom price for products in the cart

The following are 2 simple steps that can help you make it happen:

Firstly, after having your custom module installed, you declare a file named events.xml to catch an event that takes place after a product is added to the cart.

<vendor_name>/<module_name>/etc/frontend/events.xml
<?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="customPriceInCart" instance="VendorName\ModuleName\Observer\CustomPriceInCart" />
	</event>
</config>

As above you called out an observer named customPriceInCart that will handle the event. So what’s next is creating the observer to make changes to the price.

<vendor_name>/<module_name>/Observer/customPriceInCart.php
<?php
namespace VendorName\ModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class CustomPriceInCart implements ObserverInterface
{
	public function execute(\Magento\Framework\Event\Observer $observer)
	{
		$item = $observer->getEvent()->getData('quote_item');
		$item = ($item->getParentItem() ? $item->getParentItem() : $item);
		// here your custom price goes
		$customPrice = 101;
		$item->setCustomPrice($customPrice);
		$item->setOriginalCustomPrice($customPrice);
		$item->getProduct()->setIsSuperMode(true);
	}
}

By turning on super mode to the product through function setIsSuperMode(true), you stop the system generating the price then set your custom one instead.

Set custom price for products in cart in Magento 2: Replace the original price

And the result is your custom price replacing the original one whenever a product is added to the cart.

READ MORE. How to add custom command line in Magento 2 - Magenest Blog

Conclusion

The tutorial has successfully had the value of products fixed when they appear in the cart of customers. In practical business cases like parity pricing or wholesale price promotion, it would be too inconvenient to adjust one by one. You can simply put on those codes to get the price to behave as you want.

Thank you for reading the article. Please let us know if you are running well with the code above or having any trouble implementing the method.