1

I have to set the product price to a custom value depends upon the customer. So I have written an event catalog_product_get_final_price and in the observer, I have set the custom price for the product.

below is my event xml file

    <?xml version="1.0"?>
<!--
/**
 * Pawan_Catalog Module 
 *
 * @category    Catalog
 * @package     Pawan_Catalog
 * @author      pawan
 *
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_get_final_price">
        <observer name="setfinalprice" instance="Pawan\Catalog\Observer\FinalPricemanipulation" />
    </event>


</config>

My observer file is

<?php

/**
 * Pawan_Catalog Module 
 *
 * @category    checkout
 * @package     Pawan_Catalog
 * @author      pawan
 *
 */

namespace Pawan\Catalog\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class FinalPricemanipulation implements ObserverInterface {

    protected $scopeConfig;
    protected $request;

    public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Request\Http $request
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->request = $request;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/observerslogs.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $observer->getEvent()->getProduct();
        $pId = $product->getId();
        $qty = $observer->getEvent()->getQty();
        $actionName = $this->request->getFullActionName();
                $logger->info($actionName);
        if (empty($qty) || ($qty == 1)) {

            $productQuantity = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($pId);
//          $logger->info($product->getMeasurementSoldInSize() . "===" . $productQuantity->getMinSaleQty());
            if ($product->getMeasurementSoldInSize() > $productQuantity->getMinSaleQty()) {
                $finalPrice = $product->getData("final_price");
                $finalPrice *= $product->getMeasurementSoldInSize();
                $product->setFinalPrice($finalPrice);
            } elseif ($productQuantity->getMinSaleQty() > 1) {
                $actionName = $this->request->getFullActionName();
                $logger->info($actionName);
                if ($actionName == 'catalog-product-view') {
                    $finalPrice = $product->getData("final_price");
                    $finalPrice *= $productQuantity->getMinSaleQty();
                    $product->setFinalPrice($finalPrice);
                }
            }
        }
        return $this;
    }

}

but is not for view page it working when i add product to cart can any one tell why if anything i did wrong

2
  • is the Pawan\Catalog\Observer\FinalPricemanipulation class hit? ensure that the observer clas hit?
    – Amit Bera
    Commented Oct 1, 2018 at 11:31
  • yes,it was excuting in while adding to cart what i am looking is when i open product i need to set final price
    – Pawankumar
    Commented Oct 1, 2018 at 11:41

2 Answers 2

0

If it will work then there might be some other module that have overridden final price using plugin

if it won't work on default magento then there are some issues in your observer code.

Observers like:-

<?php

namespace [Vendorname]\[Modulename]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class FinalPricemanipulation implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
    {
        //logic
    }
}
5
  • one of extension using below code can pls check
    – Pawankumar
    Commented Oct 1, 2018 at 11:44
  • namespace MageWorx\OptionFeatures\Plugin; use Magento\Framework\Event\ManagerInterface; use MageWorx\OptionFeatures\Helper\Data as Helper; class AroundGetBasePrice public function aroundGetFinalPrice($subject, $proceed, $qty, $product) { $this->eventManager->dispatch('catalog_product_get_final_price', ['product' => $product, 'qty' => $qty]); $basePrice = min($product->getData('final_price'), $subject->getBasePrice($product, $qty));
    – Pawankumar
    Commented Oct 1, 2018 at 11:45
  • is this problem
    – Pawankumar
    Commented Oct 1, 2018 at 11:45
  • yes @ pawankumar
    – Divyesh
    Commented Oct 1, 2018 at 11:46
  • how can i implement for both need to work like any doc
    – Pawankumar
    Commented Oct 1, 2018 at 11:47
0

Use catalog_product_load_after event instead of catalog_product_get_final_price for magento v2.4

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.