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 SoftadroitPrescriptionObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver as EventObserver;
use PsrLogLoggerInterface;
class Orderplaceafter implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
public function __construct(
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url
) {
$this->_responseFactory = $responseFactory;
$this->_url = $url;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$event = $observer->getEvent();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$_checkoutSession = $objectManager->create('MagentoCheckoutModelSession');
$_quoteFactory = $objectManager->create('MagentoQuoteModelQuoteFactory');
//$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('MagentoCatalogModelProduct')->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 SoftadroitPrescriptionObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkEventObserver as EventObserver;
use MagentoFrameworkAppObjectManager;
use PsrLogLoggerInterface;
class Orderplaceafter implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
protected $_order;
public function __construct(
MagentoFrameworkAppResponseFactory $responseFactory,
MagentoFrameworkUrlInterface $url,
MagentoSalesApiDataOrderInterface $order
) {
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_order = $order;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$event = $observer->getEvent();
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$_checkoutSession = $objectManager->create('MagentoCheckoutModelSession');
$_quoteFactory = $objectManager->create('MagentoQuoteModelQuoteFactory');
$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();
}
}
}
2
Answers
If you are observing the event
checkout_onepage_controller_success_action
you don’t have direct access to the quote. Only to the order. And you cannot get it from the Checkout session instance either because the quote is removed from the session before the event is dispatched.But you can get the quote through the order.
you will need firts to add the quote repository as a dependency to your observer
Then, in the
execute
method you can do thisThis will get you what you are trying to do.
But I think a better idea would be to not use the quote and the products to get what you need.
You should save the product attribute value on the quote when it is added to the cart and then transfet this to the order item created from the quote item.
This way, in your observer you can do
$order = $observer->getOrder()
and then loop through the order items$order->getAllItems()
and check what you need.PLease try this below:
After that please run php bin/magento setup:upgrade