Search code examples
magentomagento2observers

Magento 2: How to get quote product id in an observer?


Product id is not displaying and through this error **{"0": "Warning: Invalid argument supplied for foreach() ** please help me to come out of this.

Here I want to return if the attribute code is not equal to 5431.

So how it is possible.

<?php

namespace Softadroit\Prescription\Observer;

use Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer as EventObserver;
use Psr\Log\LoggerInterface;

class Orderplaceafter implements ObserverInterface
{
    protected $_responseFactory;
    protected $_url;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {       
        $event = $observer->getEvent();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session');
        $_quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
        
        //$quote = $block->getQuoteData();
        /* $quote= $observer->getEvent()->getQuote();
        $item = $quote->getAllVisibleItems();
        foreach($item  as $_item){
        echo $_item->getProduct()->getId(); */
        
        $event = $observer->getEvent();
        //$item = $event->getQuoteItem();
        foreach($event->getQuoteItem()  as $_item){
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());
                
        $is_priscription = $product->getData('prescription');
        if($is_priscription != '5431'){
                return;
            }
        }
        
        if ($quote->getId()) {
            $quote->setIsActive(1)->setReservedOrderId(null)->save();
            $_checkoutSession->replaceQuote($quote);
            $url = $this->_url->getUrl('prescription/index'); //('[ModuleName]/[ModuleName]/[[Action]');
            $this->_responseFactory->create()->setRedirect($url)->sendResponse();
            die();
        }
    }
}

Any help is really appreciated

Thanks in Advance!

Next Step

This is my updated code I get Product id here, Now I want if product attribute option id is not equal to 5431 then redirect to success page i.e (order-success) and if oroduct option id is equal to 5431 then redirect to the url defined below (prescription/index)

<?php

namespace Softadroit\Prescription\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;
use \Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;

class Orderplaceafter implements ObserverInterface
{
    protected $_responseFactory;
    protected $_url;
    protected $_order;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Sales\Api\Data\OrderInterface $order
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_order = $order;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {       
        $event = $observer->getEvent();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session');
        $_quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
                    
        $orderid = $observer->getEvent()->getOrderIds();
        $order = $this->_order->load($orderid);
        
        foreach($order->getItemsCollection() as $_item){        
        $product = $_item->getProductId();  
        
        //echo $_item->getName(); die();
        
        $is_priscription =  $_item->getProduct()->getMyCustomAttribute('prescription'); 
        
        
        if($is_priscription != '5431'){
        
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setPath('order-success');
            return $resultRedirect;
            }
        }
        
        $order = $_checkoutSession->getLastRealOrder();
        $quote = $_quoteFactory->create()->loadByIdWithoutStore($order->getQuoteId());
        
        if ($quote->getId()) {
            $quote->setIsActive(1)->setReservedOrderId(null)->save();
            $_checkoutSession->replaceQuote($quote);
            $url = $this->_url->getUrl('prescription/index'); //('[ModuleName]/[ModuleName]/[[Action]');
            $this->_responseFactory->create()->setRedirect($url)->sendResponse();
            die();
        }
    }
}

Solution

  • PLease try this below:

    <?php
    
    namespace Softadroit\Prescription\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use \Magento\Framework\Event\Observer as EventObserver;
    use Psr\Log\LoggerInterface;
    
    class Orderplaceafter implements ObserverInterface
    {
        protected $_responseFactory;
        protected $_url;
    
        public function __construct(
            \Magento\Framework\App\ResponseFactory $responseFactory,
            \Magento\Framework\UrlInterface $url,
            \Magento\Quote\Model\QuoteRepository $quoteRepository,
            \Magento\Sales\Model\OrderFactory $orderFactory,
            \Magento\Checkout\Model\Session $checkoutSession
        ) {
            $this->_responseFactory = $responseFactory;
            $this->_url = $url;
            $this->quoteRepository = $quoteRepository;
            $this->orderFactory = $orderFactory;
            $this->checkoutSession = $checkoutSession;
        }
        
        public function execute(\Magento\Framework\Event\Observer $observer)
        {       
            $event = $observer->getEvent();
    
            $orderIds = $observer->getEvent()->getOrderIds();
            $order = $this->orderFactory->create()->load($orderIds[0]);
    
            $quote = $this->quoteRepository->get($order->getQuoteId());
            
            $item = $quote->getAllItems();
    
            foreach($item  as $_item){
            $product = $_item->getProduct();
                    
            $is_priscription = $product->getData('prescription');
            if($is_priscription != "" && $is_priscription == '5431'){
                    return;
                }
            }
            
            if ($quote->getId()) {
                $quote->setIsActive(1)->setReservedOrderId(null)->save();
                $this->checkoutSession->replaceQuote($quote);
                $url = $this->_url->getUrl('prescription/index'); 
                $this->_responseFactory->create()->setRedirect($url)->sendResponse();
                die();
            }
        }
    }
    

    After that please run php bin/magento setup:upgrade