I want to change the final value on getFinalPrice in Magento\Catalog\Model\Product\Type\Price so that I can modify the price based on the value of 2 customizable option text fields. I am using "$product->getCustomOption('option_' . $option->getId())" to get the value of my options on the page.

Here are my conditions with additional amounts:

  1. Field1: "Corn"

    Field2: "2 Rows"

    +$2 (add 2 dollars)

  2. Field1: "Corn"

    Field2: "3 Rows"

    +$3 (add 3 dollars)

  3. Field1: "Pesticide"

    Field2: "Airplane"

    +basePrice * 0.05 (add 5% of the basePrice)

Below is the picture of my test product with 2 text fields representing my 2 test fields (Field1 and Field2)

I feel like Magento 2 probably has an easy way to override this Price using plugins. I may make multiple copies of this plugin class so that I can also change by percentage and also be able to turn on/off customizations easily. Observers may not be ideal in this case because I need to take into account the order that I process these custom attribute prices.

Price Dependent on 2 Text Fields

What I have so far...


namespace Cpf\PriceAdjustment\Plugin\Product\Type;

class PricePlugin{
    public function __construct(
        \Psr\Log\LoggerInterface $logger
        , \Magento\Catalog\Model\ProductRepository $productRepository){
        $this->logger = $logger;
        $this->productRepository = $productRepository;
    public function aroundGetFinalPrice(\Magento\Catalog\Model\Product\Type\Price $subject, callable $proceed, $qty, \Magento\Catalog\Model\Product $product){
        $result = $proceed($qty, $product);

        $this->logger->debug(print_r($result, true));
        $finalPrice = $result;
        // $finalPrice = 2015.00;
        // $product->setFinalPrice($finalPrice);
        $id = $product->getSku();
        $this->logger->debug(print_r($id, true));
        $option_ids = $product->getCustomOption('option_ids');
            $this->logger->debug(print_r("I got some options now..." . rand(), true));
            //$this->logger->debug(print_r($option_ids, true));
            foreach (explode(',', $option_ids->getValue()) as $optionId) {
                $this->logger->debug(print_r($optionId, true));
                if ($option = $product->getOptionById($optionId)) {
                    $confItemOption = $product->getCustomOption('option_' . $option->getId());

                    $this->logger->debug(print_r($confItemOption->getValue(), true));

                    $group = $option->groupFactory($option->getType())
                    //$finalPrice += $group->getOptionPrice($confItemOption->getValue(), $basePrice);
        return $finalPrice;

1 Answer 1


What is the proper way to [...] adjust the final price base on 2 customizable option text field value?

Use the catalog_product_get_final_price event that is fired in getFinalPrice().

The product parameter is a product instance that also contains the selected custom options. You should find your custom values in $product->getCustomOptions()

  • Thank you! Is this the documentation on observers that I should follow? devdocs.magento.com/guides/v2.1/extension-dev-guide/… I'm assuming that if I use the observer, it will show the new price in cart and checkout as well? I will try it out.
    – Amad
    Aug 6, 2017 at 22:43
  • @Amad Yes and yes. Did it work? Aug 7, 2017 at 18:59
  • Yes it did. I was able to change the price. However, many google searches later, I am finding myself wanting to use plugins. Many have suggested plugins are better to future proof any customizations. The observers will work, but it does not take into account the order that observers will execute (at least not very easily). I can imagine future cases where the order (that I will process custom option price changes) will affect the price (namely percentage based pricing schemes). Thank you, though, this got me thinking on other things that cannot be overridden using plugins.
    – Amad
    Aug 7, 2017 at 20:18
  • This has help because I was able to find Magento 1 code that looked similar to what I was doing while searching for price observers. I converted it for my Magento 2 plugin class. Hopefully, soon I will have an answer that takes care of all the cases.
    – Amad
    Aug 7, 2017 at 20:25

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.