1

I used below event,

<event name="catalog_controller_product_view">
    <observer name="custom_product_view_layout" instance="Vendor\Module\Observer\CheckProduct" />
</event>

Then in my observer file,

namespace Vendor\Module\Observer;

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

class CheckProduct implements ObserverInterface 
{
  protected $_logger;
  protected $_storeManager;
  protected $messageManager;
  protected $productRepository;
  protected $_resultPageFactory;

 public function __construct(
    \Psr\Log\LoggerInterface $logger,       
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    $this->_logger = $logger;
    $this->_storeManager  = $storeManager;
    $this->messageManager = $messageManager;
    $this->_resultPageFactory = $resultPageFactory;
    $this->productRepository = $productRepository;
}

public function execute(Observer $observer) {
    $product = $observer->getEvent()->getProduct();     
    if($this->_ciicustomerHelper->isCustomerLoggedIn()) {   
        try{
            //Exam Service Api data
            $productSku = $product->getSku();
            $source_sku = $this->checkProduct($productSku);
            if(isset($source_sku) && $source_sku != false) :
                $message = "You can not buy this product";
                $this->messageManager->addError(__($message));  
            endif;              
          } catch (\Exception $ex) {
                $this->_logger->info("Message ".$ex->getMessage());
          }
    }
    return $this;
 } 
 public function checkProduct($productSku){
    $connection = $this->getConnection();
   $sql = "select * from custom_table where sku='".$productSku."'";
   $resultProduct = $connection->query($sql);
   $resultQuery = $resultProduct->fetchAll();

  if(!empty($resultQuery)){ 
    $parent_sku = $resultQuery[0]['parent_sku'];
    return true;
   }
  }else{
   return false;
  }
  }

}

This is not displaying any error message, can anyone help me on this please what is wrong in the code.

Thanks

1

You have the syntax issue.

class CheckProduct implements ObserverInterface {

    protected $_logger;
    protected $_storeManager;
    protected $messageManager;
    protected $productRepository;
    protected $_resultPageFactory;

    public function __construct(
            \Psr\Log\LoggerInterface $logger,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\Message\ManagerInterface $messageManager,
            \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->_logger = $logger;
        $this->_storeManager = $storeManager;
        $this->messageManager = $messageManager;
        $this->_resultPageFactory = $resultPageFactory;
        $this->productRepository = $productRepository;
    }

    public function execute(Observer $observer) {
        $product = $observer->getEvent()->getProduct();
        if ($this->_ciicustomerHelper->isCustomerLoggedIn()) {
            try {
                //Exam Service Api data
                $productSku = $product->getSku();
                $source_sku = $this->checkProduct($productSku);
                if (isset($source_sku) && $source_sku != false) :
                    $message = "You can not buy this product";
                    $this->messageManager->addError(__($message));
            //} //synxtax issue
            endif;
        } catch (\Exception $ex) {
            $this->_logger->info("Message " . $ex->getMessage());
        }
    }
    return $this;
}

public function checkProduct($productSku) {
    $connection = $this->getConnection();
    $sql = "select * from custom_table where sku='" . $productSku . "'";
    $resultProduct = $connection->query($sql);
    $resultQuery = $resultProduct->fetchAll();

    if (!empty($resultQuery)) {
        $parent_sku = $resultQuery[0]['parent_sku'];
        return true;
   // } //synxtax issue
    } else {
        return false;
    }
}

Also, the event is not right, you can use event catalog_controller_product_init_after

    $this->_eventManager->dispatch(
        'catalog_controller_product_init_after',
        ['product' => $product, 'controller_action' => $controller]
    );

You donot need to add \Magento\Framework\View\Result\PageFactory $resultPageFactory,remove it from __construct

Modified Code:

 <?php

namespace Vendor\Module\Observer;

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

class CheckProduct implements ObserverInterface
{
    private $_logger;
    private $_storeManager;
    private $messageManager;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->_logger = $logger;
        $this->_storeManager = $storeManager;
        $this->messageManager = $messageManager;
    }

    public function execute(Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();

        try {
            $source_sku = $this->checkProduct($product->getSku());
            if ($source_sku) {
                $message = "You can not buy this product";
                $this->messageManager->addErrorMessage(__($message));
            }
        } catch (\Exception $ex) {
            $this->_logger->info("Message " . $ex->getMessage());
        }

        return $this;
    }

    public function checkProduct($productSku)
    {
        return true;
        $connection = $this->getConnection();
        $sql = "select * from custom_table where sku='" . $productSku . "'";
        $resultProduct = $connection->query($sql);
        $resultQuery = $resultProduct->fetchAll();

        if (!empty($resultQuery)) {
            $parent_sku = $resultQuery[0]['parent_sku'];
            return true;
        } else {
            return false;
        }
    }
}
8
  • may i know where is the syntax issue pls? @Amit Sep 13 '19 at 12:30
  • I already commented.Please any good editor Link NetBeans and sublime
    – Amit Bera
    Sep 13 '19 at 12:33
  • Yes, I resolved syntax error, then tried still error message not showing in product detail page, can i change the event? Sep 13 '19 at 12:35
  • even i tried with your suggest event, still error message is not shown. Sep 13 '19 at 12:39
  • Still its not showing, I modified the code as per your suggestion Sep 13 '19 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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