Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

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,

chang_price_1

On Product View page

change_price_2

On Catalog Search page

change_price_3

On Catalog Category page

change_price_4

If you want to set custom price when adding product to cart then checkout Magento2 – Set custom price of Product when adding to cart

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Vinay
    • Kartik Upadhyay (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home