Magento Tutorials

How to Change Product Price with Plugin in Magento 2

Hello, Magento Friends!

Today, I am here to guide you all about How to Change Product Price with Plugins in Magento 2.

Primarily, when you have just started your Magento 2 store and wish to change the price of the product then changing the product price with a plugin in Magento 2 could be an alternative option to seek! Normally, there are various methods of changing a product price. You can use the Event – observe or “Rewrite” if the Event is not supported at that time. Consequently, in “rewrite” you won’t be able to make a change so that will create a dispute.

Let’s dive right into the steps to Change Product Price with Plugin in Magento 2!

Steps to Change Product Price with Plugin in Magento 2

Step 1: Create a di.xml file in the below-given path

app\code\Vendor\Extension\etc\frontend\di.xml

Now, add the below-given code to the created file.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

   <type name="Magento\Catalog\Model\Product">

    <plugin name="change_product_price" type="Vendor\Extension\Plugin\Productprice" disabled="true" sortOrder="1" />

  </type>   

</config>

Step 2: Create a Productprice.php file in the below path.

app\code\Vendor\Extension\Plugin\Productprice.php

Now, add the below-given code to the created file.

<?php

namespace Vendor\Extension\Plugin;

 

class Productprice

{

    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)

    {

$result += 150; //add your product price logic

        return $result; 

    }

}

Bottom Line:

So, this was it for the steps of How to Change Product Price with Plugin in Magento 2. Attach the above code and you will get the desired results. Moreover, integrate the Magento 2 Custom Price Extension for your Magento 2 store which allows the store admin to set a base price and the customers to add their desired product price while making a purchase.

Also, if you encounter any difficulties, make yourself comfortable and drop in your problems in the comments section below! Share this article with your Magento buddies! See you until our next tutorial blog.

Have a G’day!

Happy Reading!

Click to rate this post!
[Total: 7 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

9 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago