I have created payment method module. In this module when i was logged in, it was successfully got order id from Magento\Quote\Api\CartManagementInterface interface. but when i was guest, i was not able to get order id from Magento\Quote\Api\CartManagementInterface interface. I was search in google and found that, for guest customer interface was changed Magento\Quote\Api\GuestCartManagementInterface. I have also try this interface, but still not working for get order id of guest customer. When i have click on place order button, My module controller is called. My controller code is given below.
<?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();
}
}
}
?>
How can I get order id for guest customer in controller after click on place order button ? Please help me. Any help would be appreciated.
Thanks
Is the order correctly registered for guest customers ? Do you have any exceptions ?
Maybe you can try this :
$quote->setCustomerId(null)
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
$quote->collectTotals();
$orderId = $this->cartManagement->placeOrder($quote->getId());
I never tested my code without it though, but no matter the type of customer I always have my order id.