In this tutorial, we'll explore how to dynamically change the price of a specific product when a customer updates their shopping cart in Magento 2.4.5. This feature is handy when you want to apply custom pricing rules or offer special discounts to your customers based on certain conditions. Let's dive into this step-by-step guide with practical examples!
Step 1: Create a Custom Module
First, let's create a custom module to handle the price change functionality. Create a new directory for your module under app/code
and name it something meaningful, such as CustomPriceModifier
.
Step 2: Define the Module Structure
Within your module directory, create the following subdirectories and files:
CustomPriceModifier
├── etc
│ └── module.xml
├── registration.php
├── composer.json
Step 3: Define the Module Configuration
In the etc
directory, create the module.xml
file with the following content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="CustomPriceModifier" setup_version="1.0.0">
<sequence>
<module name="Magento_Quote"/>
</sequence>
</module>
</config>
Step 4: Register the Module
In the module's root directory, create the registration.php
file:
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'CustomPriceModifier', __DIR__);
Step 5: Implement the Price Modifier
Now, let's create a plugin to modify the product price. In the etc
directory, create the di.xml
file with the following content:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Item">
<plugin name="custom_price_modifier" type="CustomPriceModifier\Plugin\QuoteItemPlugin" sortOrder="1"/>
</type>
</config>
Step 6: Create the QuoteItemPlugin
In the Plugin
directory of your module, create the QuoteItemPlugin.php
file with the following code:
<?php
namespace CustomPriceModifier\Plugin;
class QuoteItemPlugin
{
public function afterGetPrice(\Magento\Quote\Model\Quote\Item $subject, $result)
{
// Here, you can implement your custom logic to change the product price.
// For example, let's reduce the price by 10% for a specific product with ID 123.
$productIdToModify = 123;
$discountPercentage = 10;
if ($subject->getProductId() === $productIdToModify) {
$result = $result - ($result * $discountPercentage / 100);
}
return $result;
}
}
Step 7: Enable the Custom Module
Finally, enable your custom module by running the following Magento CLI command:
php bin/magento module:enable CustomPriceModifier
And then, run the setup upgrade to apply the changes:
php bin/magento setup:upgrade
Step 8: Test the Price Modifier
Now, visit your Magento store, add the product with ID 123 (or the one you specified) to the cart, and update the cart. You should see that the product price is reduced by the specified percentage.
Congratulations! 🎉 You've successfully implemented a dynamic price change for a specific product when a customer updates their shopping cart in Magento 2.4