Firstly, we have to create a custom module.
Then, we have to declare a file named events.xml to catch an event that takes place after a product is added to the cart.
Create events.xml file at the following path Magemonkeys/CustomPriceInCart/etc/frontend/events.xml
1 2 3 4 5 6 |
<?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="Magemonkeys\CustomPriceInCart\Observer\CustomPriceInCart" /> </event> </config> |
The next step is to create the observer to make changes to the price.
Create CustomPriceInCart.php file at the following path Magemonkeys/CustomPriceInCart/Observer/CustomPriceInCart.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace Magemonkeys\CustomPriceInCart\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), we can stop the system generating the price.
Then we can set the custom prices for products in the cart.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62837ac0bc5ae534006286/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...