0

I want to change the special price when it get displayed on the frontend. so, if the special price is $25 then I want it to show $(100+25). How can I achieve this. I created a plugin which update the afterGetPrice() (works for regular price only).

Is there any way to change the special Price/tier price on the frontend only. (It must checkout with the updated price-- $125)

my 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" type="vendor\module\Plugin\Model\Product" sortOrder="1" />
     </type>
 </config>

and Plugin/Model/Product.php

<?php

namespace vendor\module\Plugin\Model;

class Product
  {

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

       return $result * 2.25;
      }
   }

which display the product with the wanted price but this piece of code does not work if product has special price..I want to do the same for all price types.

2 Answers 2

0

I understand your plugin already works. You are now trying to apply the plugin price change only for frontend. Try the below then, that should do just that

use Magento\Framework\App\Area;
use Magento\Framework\App\State;

class PricePlugin
{
    /**
     * @var State
     */
    private $state;

    public function __construct(
        State $state
    ) {
        $this->state = $state;
    }

    public function afterGetPrice(
        \Magento\Catalog\Model\Product\Type\Price $price,
        $result,
        \Magento\Catalog\Model\Product $product
    ) {
        if ($this->state->getAreaCode() === Area::AREA_FRONTEND) {
            $result += 100; //  charge for frontend user
        }

        return $result;
    }
}

in your case, change

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

       return $result * 2.25;
      }

to

public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
{ 
       if ($this->state->getAreaCode() === Area::AREA_FRONTEND) {
               $result * 2.25
            }

       return $result;
}

of course, add state dependency in your constructor like in my example

7
  • Hi @Herve thanks for the answer. I am a bit confuse now, do I need to create a new plugin file?
    – Nit
    Commented Oct 9, 2020 at 11:20
  • 1
    no your plugin file should be fine, update your question with it to be sure maybe. Put all your code just to be clear.. Commented Oct 9, 2020 at 11:22
  • I have updated the code.
    – Nit
    Commented Oct 9, 2020 at 11:31
  • okay I am trying this.
    – Nit
    Commented Oct 9, 2020 at 11:37
  • please imgur.com/jDQcCHZ visit this
    – Nit
    Commented Oct 9, 2020 at 11:46
0

In case someone need this:

all you need to just copy the same function and change the name afterGetSpecialPrice:

public function afterGetSpecialPrice(\Magento\Catalog\Model\Product $subject, $result)
{ 
   return $result * 2.25;
}

don't forget to run setup:upgrade command and cache:clean good day :)

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.