Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to change product price with Plugin in Magento 2

Vinh Jacker | 03-17-2025

Change product price with Plugin

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

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services