Magento 2

How to Override Frontend Store Product Price in Magento 2

Pricing is simple but it will make or break your business in many ways. Setting up high price leads to cart abandon and setting up law may result in you in loss of money. That’s why Pricing strategies are a mix of art and science. Assigning an Optimal price to a product each product, it’s important that can both improve your profits and increase store customer satisfaction should be increased sequentially.

Recently, a client came to us with a requirement of increasing all store product price by 15% without changing each product price individually from store backend. After thinking for a while, we pop out with a plan to develop a custom extension and increase store product price by overriding actual price of a product. Follow these 3 easy steps and you are done.

First Create ‘Module.xml’ file at below location using following code. It will help to override our custom layout file.
app\code\Vendor\Extension\etc\module.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="Vendor_Extension" setup_version="1.0.0" active="true">
     <sequence>
         <module name="Magento_Catalog"/>
         <module name="Magento_CatalogWidget"/>
         <module name="Magento_Widget"/>
     </sequence>
 </module>
</config>
</pre>

Now it’s time to create our own custom layout at following path using this code.
app\code\Vendor\Extension\view\base\layout\catalog_product_prices.xml

<pre class="lang:default decode:true">
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
 <block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
     <arguments>
            <argument name="default" xsi:type="array">
             <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
             <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
             <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
               <strong><item name="default_amount_render_template" xsi:type="string">Vendor_Extension::catalog/product/price/amount/default.phtml</item></strong>
             <item name="prices" xsi:type="array">
                 <item name="special_price" xsi:type="array">
                     <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                 </item>
                 <item name="tier_price" xsi:type="array">
                     <item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item>
                 </item>
                 <item name="final_price" xsi:type="array">
                     <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item>
                     <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item>
                 </item>
                 <item name="custom_option_price" xsi:type="array">
                     <item name="amount_render_template" xsi:type="string">Magento_Catalog::product/price/amount/default.phtml</item>
                 </item>
                 <item name="configured_price" xsi:type="array">
                     <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\ConfiguredPriceBox</item>
                     <item name="render_template" xsi:type="string">Magento_Catalog::product/price/configured_price.phtml</item>
                 </item>
             </item>
             <!--<item name="adjustments" xsi:type="array"></item>-->
         </argument>
     </arguments>
 </block>
</layout>
</pre>

Lastly, we have to create one more file ‘default.phtml’ with our logic of overriding default price.
app\code\Vendor\Extension\view\base\templates\catalog\product\price\amount\default.phtml

<pre class="lang:default decode:true">
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$price_arr = explode('-', $block->getPriceId());
foreach ($price_arr as $item)
{
 if(is_numeric($item)){
     $pid=$item;
     break;
 }
}
$product = $om->get('Magento\Catalog\Model\ProductFactory')->create()->load($pid);
$priceHelper = $om->create('Magento\Framework\Pricing\Helper\Data'); ?>
<span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
 <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
<?php if ($block->getDisplayLabel()): ?>
 <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
<?php endif; ?>
 <?php $ finalprice =$product->getFinalPrice();
     $finalprice+=(($finalprice*15)/100); ?>
 <span class="price-label">
<h4>
<b><?php echo $priceHelper->currency($finalprice, true, false);  ?></b>
</h4>
</span>
</span>
</pre>

You can change $finalprice equation as per your requirement of increasing or decreasing price. Also, you can play & manipulate with these codes according to your needs.

Lastly, hit that stars if you love reading our blogs and comment down below if you face any issue while using this code.
Happy Overriding!

Click to rate this post!
[Total: 15 Average: 4.1]
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.🏏

View Comments

  • you shouldn't be using ObjectManager. Could you update your solution using any helper instead?

    We are using Magento 2, NOT Magento 1, so please can you give us a solution using dependency injection instead the ObjectManager?

    • Yes, you can pass the required helper into the XML as an argument and you can use those into phtml.

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

8 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago