【问题标题】:Magento 2: How to get quote product id in an observer?Magento 2:如何在观察者中获取报价产品 ID?
【发布时间】:2020-12-15 11:58:06
【问题描述】:

产品 ID 未显示,并通过此错误 **{"0": "Warning: Invalid argument provided for foreach() ** 请帮我解决这个问题。

这里如果属性码不等于5431我想返回。

那怎么可能呢。

<?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();
        }
    }
}

非常感谢任何帮助

提前致谢!

下一步

这是我更新的代码,我在这里得到产品 id,现在我想如果产品属性选项 id 不等于 5431 然后重定向到成功页面,即(订单成功),如果 oroduct 选项 id 等于 5431 然后重定向到下面定义的网址(处方/索引)

<?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();
        }
    }
}

【问题讨论】:

  • 观察者事件名称是什么
  • 观察者名称:softadroit_order_success & 事件名称:checkout_onepage_controller_success_action

标签: magento magento2 observers


【解决方案1】:

如果您正在观察事件checkout_onepage_controller_success_action,则您无法直接访问报价。只对订单。而且您也无法从 Checkout 会话实例中获取它,因为在调度事件之前从会话中删除了报价。
但是您可以通过订单获取报价。

您需要先将引用存储库作为依赖项添加到您的观察者

private $quoteRepository;
public function __construct(
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
    ....
) {
    $this->quoteRepository = $quoteRepository;
    ....
}

然后,在execute 方法中你可以这样做

$order = $observer->getOrder();
$quoteId = $order->getQuoteId();
$quote = $this->qupteRepository->get($quoteId);
if ($quote && $quote->getId()) {
    $items = $quote->getAllItems();
    //loop through items ....
}

这会让你得到你想要做的事情。
但我认为一个更好的主意是不使用报价和产品来获得您需要的东西。
您应该在将报价单添加到购物车时将产品属性值保存在报价单上,然后将其转移到从报价单项创建的订单项中。
这样,在您的观察者中,您可以执行$order = $observer-&gt;getOrder(),然后遍历订单项$order-&gt;getAllItems() 并检查您需要什么。

【讨论】:

  • if ($quote && $quote->getId()) { $items = $quote->getAllItems(); //循环遍历项目.... } 如果我必须更新项目名称,那么我应该做什么。请帮忙。
【解决方案2】:

请在下面试试这个:

<?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();
        }
    }
}

之后请运行php bin/magento setup:upgrade

【讨论】:

  • {"0":"创建对象时发生类型错误:Softadroit\\Prescription\\Observer\\Orderplaceafter","1" 彻底解决此错误
  • 请运行 php bin/magento setup:upgrade
  • 再次重定向到同一页面
  • $is_priscription = $_item-&gt;getProduct()-&gt;getMyCustomAttribute('prescription');这在echo中没有得到任何值,我认为这是问题所在,请您检查一下
  • 这样就可以得到这个数据 $is_priscription = $_item->getProduct()->getData('prescription');或 $is_priscription = $_item->getProduct()->getPrescription();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2014-07-15
  • 1970-01-01
  • 2023-03-31
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多