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
For displaying product prices on product pages or widgets, Magento calculates the lowest price from one of the base price, special price, tier price, or catalog rule price. Configurable products can set their own prices for child products and bundle products can apply a fixed price on the bundle. Then when adding the product to the cart its price can be affected by cart price rules too.
In this article, we will show how to add your custom price to a product in the cart. This action will be overriding the price Magento has calculated.
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>
Here you declare an observer named customPriceInCart that will handle the event checkout_cart_product_add_after which Magento dispatches every time a product’s added to the cart.
Next up, we create the observer class:
<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) { //get the item just added to cart $item = $observer->getEvent()->getData('quote_item'); //(optional) get the parent item, if exists $item = ($item->getParentItem() ? $item->getParentItem() : $item); // set your custom price $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.
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
Conclusion
The tutorial has successfully had the value of products fixed when they appear in the cart of customers. Aside from setting a fixed number, you can add your own functions and inject other classes into the observer for custom pricing logics.
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.