Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Magento 2:单击下订单按钮后,如何在controller中为来宾客户获取订单id?_Php_Magento_Magento2 - Fatal编程技术网

Php Magento 2:单击下订单按钮后,如何在controller中为来宾客户获取订单id?

Php Magento 2:单击下订单按钮后,如何在controller中为来宾客户获取订单id?,php,magento,magento2,Php,Magento,Magento2,我已经创建了付款方式模块。在我登录的这个模块中,它成功地从Magento\Quote\Api\CartManagementInterface界面获取了订单id。但当我是客人时,我无法从Magento\Quote\Api\CartManagementInterface界面获取订单id。 我在谷歌上搜索发现,对于guest,客户界面已更改Magento\Quote\Api\GuestCartManagementInterface。我也尝试过这个界面,但仍然无法获取客户的订单id。 当我点击下订单按钮

我已经创建了付款方式模块。在我登录的这个模块中,它成功地从Magento\Quote\Api\CartManagementInterface界面获取了订单id。但当我是客人时,我无法从Magento\Quote\Api\CartManagementInterface界面获取订单id。 我在谷歌上搜索发现,对于guest,客户界面已更改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。

在成功页面上,magento也会自动为客户用户显示订单id。或者您可以使用会话获取订单id。
Mage::getSingleton('checkout/session')->getLastOrderId()我正在使用magento 2。我还试图从签出会话获取订单id,但它将返回空白:$this->\u checkoutSession->getlastreamorderid();我的控制器在成功页面之前调用,因此我无法获取订单对象