How to change product price with Plugin in Magento 2
Using a plugin to change the product price in Magento 2 is one of the great alternative choices when you start with Magento 2 store, and have a tendency to change something like product price.
Using a plugin to change the product price in Magento 2 is one of the great alternative choices when you start with Magento 2 store, and have a tendency to change something like product price. As normally, there are many ways to modify the product price, maybe you know that you will use the Event - Observe
or “Rewrite” if the “Event” is not supported at that time. However, the Rewrite doesn’t accept the customization from two modules in the same file, that will cause the conflict and you can’t change anything.
Coming with Magento 2 tutorial, this article will demonstrate how to change the product price with plugin via the steps.
Overview of changing product price with plugin in Magento 2 in three easy-to-follow steps
Follow this guide and make your life easier when it comes to changing prices of any products you want in the future.
Let’s dive in!
Step 1: Add di.xml
file
It is crucial to identify where the place you want to activate the plugin, and then add di.xml
file. Here, the sample is the frontend area with the following content for the di.xml
file.
<config>
<type name="Magento\Catalog\Model\Product">
<plugin name="change_product" type=" Mageplaza\HelloWorld\Plugin\Product " sortOrder="1" disabled="true"/>
</type>
</config>
Step 2: Change the product price
Open folder “Mageplaza\HelloWorld\Plugin” to add new class Product.php
. Please put the prefix “after” before one of two ways to change the product price:
- Edit the values reported by an original method
- Call some behaviors once an original method is called.
And the result when the original method getPrice()
finishes is declared by the $result
. For example, you will adjust the product price by multiply it with 2.
<?php
namespace Mageplaza\HelloWorld\Plugin;
class Product
{
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
{
return $result + 100;
}
}
When step 2 has done, this means you can see the customization of the product price on the frontend. Hope you find it useful within running Magento 2 store.
Step 3: Flush Cache and Test
Flush cache and test the result
Final words
That’s all you need to do to change product price in Magento 2. Run the above code and you will get the result. By integrating Magento 2 plugin, it will be easier to customize the product price and you will not have to encounter any limitations of the Magento Default.
I hope you find this tutorial helpful. Thanks for reading!
Related Topic
Enjoyed the tutorial? Spread it to your friends!
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
People also searched for
- magento 2 how to change product price with plugin
- 2.2.x, 2.3.x, 2.4.x