Shopping cart

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. 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 make the conflict and you can’t change anything.

Coming with Magento 2 tutorial, I will teach you how to change the product price with plugin via the steps.

Overview of changing product price with plugin in Magento 2

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.

  <type name="Magento\Catalog\Model\Product">
    <plugin name="change_product" type=" Mageplaza\HelloWorld\Plugin\Product " sortOrder="1" disabled="true"/>

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.

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

Related Topic

Enjoyed the tutorial? Spread it to your friends!


Sam Thomas
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 a wanna-be influencer. He is a big fan of sports and travel, also.

Comments for How to change product price with Plugin in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

People also searched for

  • magento 2 how to change product price with plugin
  • 2.1.x, 2.2.x, 2.3.x