1

I'm trying to update/override the product price using the code below.

frontend/di.xml file

<?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="ABCSolutions\ProductTab\Plugin\Model\Product" sortOrder="1" />
    </type>

</config>

And the Product.php file code

<?php
namespace ABCSolutions\ProductTab\Plugin\Model;

class Product
{

    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        if($subject->getSku() == 'B2BTest'){
            $result += 10;
        }
        return $result;
    }

}

It is applied when the product does not have Tier Prices as shown below. The product cost is 10 and I added 10 so it is 20. enter image description here

But when configuring tier prices, Magento overrides the price to show as tier price as the final price. In this case, the above code is not working. enter image description here

So, I tried to override the \Magento\Catalog\Pricing\Render\FinalPriceBox class of wrapResult() method. When I debug the code the calculation is already done somewhere.

So, which file should override to calculate and apply or apply custom price?

I customized the afterGetPrice method and set the price based on the BC API call. And this is working without tier and special price. So which method should we override to set the custom price if tier and special prices are configured?

We are using this logic/setting price for only the product page, not for any other pages. So, we believe it will not cause any issues.

4 Answers 4

3

To solve this, you need to create a plugin for the getFinalPrice method instead of getPrice. The getFinalPrice method is responsible for calculating the final price of a product, taking into account all types of discounts, including tier prices.

<?php
namespace ABCSolutions\ProductTab\Plugin\Model;

class Product
{
    public function afterGetFinalPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        if($subject->getSku() == '24-MB03'){
            $result += 4;
        }
        return $result;
    }
}

Here, we're using an after plugin (afterGetFinalPrice) which allows us to interact with the result of the getFinalPrice method after it's returned. This should work even when tier prices are set.

Reference : vendor/magento/module-catalog/Model/Product.php


Update:

This will add your custom price to the price when you add to the cart the product and it will reflect at all places in the cart, quote, checkout, etc.

For finalPriceBox You need to override the below file which will show the price as per your customization to the front user.

For time-saving I have added to the core files directly to show case your demo. So, Please override it and then do customization once you are satisfied with the result.

File : vendor/magento/module-catalog/Pricing/Price/TierPrice.php

Function: getStoredTierPrices()

protected function getStoredTierPrices()
{
    if (null === $this->rawPriceList) {
        $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
        if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
            /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
            $attribute = $this->product->getResource()->getAttribute(self::PRICE_CODE);
            if ($attribute) {
                $attribute->getBackend()->afterLoad($this->product);
                $this->rawPriceList = $this->product->getData(self::PRICE_CODE);
            }
        }
        if (null === $this->rawPriceList || !is_array($this->rawPriceList)) {
            $this->rawPriceList = [];
        }
        if (!$this->isPercentageDiscount()) {
            foreach ($this->rawPriceList as $index => $rawPrice) {
                if (isset($rawPrice['price'])) {

                    //Your required logic goes here
                    if ($this->product->getSku() == '24-MB03') {
                        $rawPrice['price'] += 4;
                    }
                    //Your required logic goes here - End

                    $this->rawPriceList[$index]['price'] =
                        $this->priceCurrency->convertAndRound($rawPrice['price']);
                }
                if (isset($rawPrice['website_price'])) {
                    $this->rawPriceList[$index]['website_price'] =
                        $this->priceCurrency->convertAndRound($rawPrice['website_price']);
                }
            }
        }
    }
    return $this->rawPriceList;
}

The getStoredTierPrices() function in PHP retrieves the tier prices associated with a product from the database. You can override it and update as per requirement.

enter image description here

I have tested it in my Magento 2.4.6 setup.

Configured Tier Price

Configured Tier Price

Before fulfilling the pricing criteria and before adding to the cart

Before Full filling the pricing criteria and before add to cart

After adding a Product with 100 Quantities which will consider tier-price

After Adding Product with 100 Quantity which will consider tier - price in my case

Cart Page

enter image description here

Please do not forget to flush the cache while testing, as the page is being cached and not calling this function every time.

3
  • Tried with the above 2 methods as around and after and not working for me. However, my customized method afterGetPrice is firing debug. But not for the above 2 methods.
    – Bojjaiah
    Commented May 24 at 9:30
  • Hello @Bojjaiah, I have updated my answer with the test results, Please do not forget to override the files that are mentioned, and approve this answer if it is what you are looking for. Thanks! Commented May 24 at 17:58
  • Thank you for the answer. The above solution will work when tier price has. What if any tier price and special price are configured? will it work
    – Bojjaiah
    Commented May 26 at 16:09
1
+250

If you have to change the prices for a group of customers, then you are going about this process wrong (I am saying this as you are setting b2b price). This would generate quite some issues and mostly related to caching.

You should be setting tier prices instead of custom price or plugin on getFinalPrice(). This is because the cache in Magento is segmented based on tier price, so each tier has it's own copy in the cache storage.

Also, you should be setting prices in cron or any background process so it doesn't have to be set over and over again when customer asks for price.

8
  • I customized the afterGetPrice method set price based on the BC API call. And this is working without tier and special price. So which method should we override to set the custom price if tier and special prices are configured?
    – Bojjaiah
    Commented May 27 at 8:31
  • I will have to better understand your use case to give you a good solution but if you must have to call the API on each product load then you are better off not setting the prices in Magento at all as that would be waste of resources because you are calling the API either way. Just show the prices using api call (ajax) and set custom price during add to cart and most importantly do the api call in ajax so it doesn't get cached. You can see how to set custom price here - magento.stackexchange.com/questions/351800/… Commented May 27 at 12:19
  • We are using this logic/setting price for only the product page, not for any other pages. So, we belive it will not cause any issues.
    – Bojjaiah
    Commented May 27 at 16:46
  • 1
    Product pages are also cached so you should be getting and showing the price using ajax call and setting it as custom price during add to cart. Commented May 28 at 4:52
  • Thank you for the clarification. I have seen the above-shared link, I think that way works and have to test it. Can you please suggest, which method should we override to set a custom price, if the product has special and tier prices? I just wanted to know.
    – Bojjaiah
    Commented May 28 at 5:20
0

To override the product price including tier pricing in Magento 2, you need to target the correct price calculation logic. The afterGetPrice method doesn't handle tier pricing. Instead, you can override the getFinalPrice method, which considers all price rules including tier prices.

Here is how to update your plugin:

frontend/di.xml:

    <?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_final_price" type="ABCSolutions\ProductTab\Plugin\Model\Product" sortOrder="1" />
    </type>
</config>

Product.php:

     <?php
namespace ABCSolutions\ProductTab\Plugin\Model;

class Product
{
    public function afterGetFinalPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        if ($subject->getSku() == 'B2BTest') {
            $result += 10;
        }
        return $result;
    }
}

di.xml: Defines a plugin for the Magento\Catalog\Model\Product class. Product.php: Implements the afterGetFinalPrice method to add your custom price logic after the final price is calculated, ensuring it applies regardless of tier pricing.

This approach ensures that your price adjustment is included in the final price calculation, which includes tier pricing logic. If you still encounter issues, consider debugging getFinalPrice in Magento\Catalog\Model\Product to understand the exact flow and ensure your changes take effect as expected.

2
0

di.xml: Create the di.xml file in the app/code/Vendor/Module/etc directory. xml

<?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="custom_price_plugin" type="Vendor\Module\Plugin\ProductPlugin" /> </type> </config>

ProductPlugin.php: Create the ProductPlugin.php file in theapp/code/Vendor/Module/Plugin directory. php

<?php namespace Vendor\Module\Plugin; use Magento\Catalog\Model\Product; class ProductPlugin { /** * @param Product $subject * @param float $result * @return float */ public function afterGetPrice(Product $subject, $result) { // Modify the price as needed $customPrice = $result + 10; // Example: add 10 to the original price return $customPrice; } }

Try This Method

1
  • Did you check my thread? read fully and comment here
    – Bojjaiah
    Commented May 30 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.