繁体   English   中英

Magento 2:单击下订单按钮后,如何在控制器中获取访客客户的订单ID?

[英]Magento 2 : How to get order id for a guest customer in controller after click on place order button?

我已经创建了付款方式模块。 在此模块中,当我登录时,已从Magento \\ Quote \\ Api \\ CartManagementInterface接口成功获取了订单ID。 但是当我做客时,我无法从Magento \\ Quote \\ Api \\ CartManagementInterface界面获取订单ID。 我在google中搜索时发现,对于来宾客户界面, Magento \\ Quote \\ Api \\ GuestCartManagementInterface已更改。 我也尝试过该界面,但仍无法获得来宾客户的订单ID。 当我单击下订单按钮时​​,将调用我的模块控制器。 我的控制器代码如下。

<?php 

namespace Mageniks\Testpayment\Controller\Payment; 


use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\GuestCartManagementInterface;

class Redirect extends \Magento\Framework\App\Action\Action 
{
    /**
     * Customer session model
     *
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;
    protected $resultPageFactory;
    protected $_paymentMethod;
    protected $_checkoutSession;
    protected $checkout;
    protected $cartManagement;
    protected $guestcartManagement;
    protected $orderRepository;
    protected $_scopeConfig;
    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Mageniks\Testpayment\Model\PaymentMethod $paymentMethod,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        CartManagementInterface $cartManagement,
        GuestCartManagementInterface $guestcartManagement

    ) {
        $this->_customerSession = $customerSession;
        parent::__construct($context);
        $this->_paymentMethod = $paymentMethod;
        $this->_checkoutSession = $checkoutSession;
        $this->cartManagement = $cartManagement;
        $this->guestcartManagement = $guestcartManagement;
        $this->orderRepository = $orderRepository;
        $this->_scopeConfig = $scopeConfig;


    }

    public function execute()
    {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        $orderId = '';
        if($customerSession->isLoggedIn()) 
        {
            // FOR LOGIN CUSTOMER GET ORDER ID

              $orderId = $this->cartManagement->placeOrder($this->_checkoutSession->getQuote()->getId());
        }
        else
        {
                  // FOR GUEST CUSTOMER GET ORDER ID

                   try 
                   {
                      $orderId = $this->guestcartManagement->placeOrder($this->_checkoutSession->getQuote()->getId());

                    } catch (\Exception $e) 
                    {
                        echo $e->getMessage();
                    }

         }

        $order = $this->orderRepository->get($orderId);
        if ($order){
            $order->setState($this->_scopeConfig->getValue('payment/testpayment/new_order_status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
            $order->setStatus($this->_scopeConfig->getValue('payment/testpayment/new_order_status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE));
            $order->save();
        }

    }

}

?>

单击下订单按钮后,如何在控制器中获取访客客户的订单ID? 请帮我。 任何帮助,将不胜感激。

谢谢

订单是否已正确为访客客户注册? 你有例外吗?

也许你可以试试这个:

$quote->setCustomerId(null)
        ->setCustomerEmail($quote->getBillingAddress()->getEmail())
        ->setCustomerIsGuest(true)
        ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
$quote->collectTotals();
$orderId = $this->cartManagement->placeOrder($quote->getId());

我从来没有测试过没有它的代码,但是无论客户类型如何,我总是有我的订单ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM