title: Magento 2: How to get quote product id in an observer?
body: <p>Product id is not displaying and through this error **{&quot;0&quot;: &quot;Warning: Invalid argument supplied for foreach() ** please help me to come out of this.</p>
  <p>Here I want to return if the attribute code is not equal to 5431.</p>
  <p>So how it is possible.</p>
  <pre><code>&lt;?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-&gt;_responseFactory = $responseFactory;
          $this-&gt;_url = $url;
      }
      
      public function execute(\Magento\Framework\Event\Observer $observer)
      {       
          $event = $observer-&gt;getEvent();
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $_checkoutSession = $objectManager-&gt;create('\Magento\Checkout\Model\Session');
          $_quoteFactory = $objectManager-&gt;create('\Magento\Quote\Model\QuoteFactory');
          
          //$quote = $block-&gt;getQuoteData();
          /* $quote= $observer-&gt;getEvent()-&gt;getQuote();
          $item = $quote-&gt;getAllVisibleItems();
          foreach($item  as $_item){
          echo $_item-&gt;getProduct()-&gt;getId(); */
          
          $event = $observer-&gt;getEvent();
          //$item = $event-&gt;getQuoteItem();
          foreach($event-&gt;getQuoteItem()  as $_item){
          $product = $objectManager-&gt;create('Magento\Catalog\Model\Product')-&gt;load($_item-&gt;getProductId());
                  
          $is_priscription = $product-&gt;getData('prescription');
          if($is_priscription != '5431'){
                  return;
              }
          }
          
          if ($quote-&gt;getId()) {
              $quote-&gt;setIsActive(1)-&gt;setReservedOrderId(null)-&gt;save();
              $_checkoutSession-&gt;replaceQuote($quote);
              $url = $this-&gt;_url-&gt;getUrl('prescription/index'); //('[ModuleName]/[ModuleName]/[[Action]');
              $this-&gt;_responseFactory-&gt;create()-&gt;setRedirect($url)-&gt;sendResponse();
              die();
          }
      }
  }
  
  </code></pre>
  <p>Any help is really appreciated</p>
  <p>Thanks in Advance!</p>
  <p><strong>Next Step</strong></p>
  <p>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)</p>
  <pre><code>&lt;?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-&gt;_responseFactory = $responseFactory;
          $this-&gt;_url = $url;
          $this-&gt;_order = $order;
      }
      
      public function execute(\Magento\Framework\Event\Observer $observer)
      {       
          $event = $observer-&gt;getEvent();
          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $_checkoutSession = $objectManager-&gt;create('\Magento\Checkout\Model\Session');
          $_quoteFactory = $objectManager-&gt;create('\Magento\Quote\Model\QuoteFactory');
                      
          $orderid = $observer-&gt;getEvent()-&gt;getOrderIds();
          $order = $this-&gt;_order-&gt;load($orderid);
          
          foreach($order-&gt;getItemsCollection() as $_item){        
          $product = $_item-&gt;getProductId();  
          
          //echo $_item-&gt;getName(); die();
          
          $is_priscription =  $_item-&gt;getProduct()-&gt;getMyCustomAttribute('prescription'); 
          
          
          if($is_priscription != '5431'){
          
              $resultRedirect = $this-&gt;resultFactory-&gt;create(ResultFactory::TYPE_REDIRECT);
              $resultRedirect-&gt;setPath('order-success');
              return $resultRedirect;
              }
          }
          
          $order = $_checkoutSession-&gt;getLastRealOrder();
          $quote = $_quoteFactory-&gt;create()-&gt;loadByIdWithoutStore($order-&gt;getQuoteId());
          
          if ($quote-&gt;getId()) {
              $quote-&gt;setIsActive(1)-&gt;setReservedOrderId(null)-&gt;save();
              $_checkoutSession-&gt;replaceQuote($quote);
              $url = $this-&gt;_url-&gt;getUrl('prescription/index'); //('[ModuleName]/[ModuleName]/[[Action]');
              $this-&gt;_responseFactory-&gt;create()-&gt;setRedirect($url)-&gt;sendResponse();
              die();
          }
      }
  }
  </code></pre>