Set Custom Product Price when displaying on Front-end in Magento 2
In this blog, we will learn how to change the product price when displaying on the front-end.
First, create ‘di.xml’ under Webkul/ChangePrice/etc/frontend/
<?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\Catalog\Model\Product">
<plugin name="change_product" type="Webkul\ChangePrice\Plugin\Model\Product" sortOrder="1" />
</type>
</config>
Then create ‘Product.php’ under Webkul/ChangePrice/Plugin/Model/
<?php
namespace Webkul\ChangePrice\Plugin\Model;
class Product
{
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
{
$result += 10;
return $result;
}
}
In the above example we have added 10 to every product.
This will reflect on all the front-end pages i.e. Product View page, Catalog Search page, Catalog Category page.
We created a product of $50,
On Product View page
On Catalog Search page
On Catalog Category page
If you want to set custom price when adding product to cart then checkout Magento2 – Set custom price of Product when adding to cart
Categories:
Magento2
Tags:
Change Product price programatically on Front-end Set Custom Product Price when displaying on Front-end
View Comments (2)
Comment or Ask a Question
Quick Links