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