M2支付失败还原购物车

3.04K 浏览开发笔记

M2支付失败还原购物车

在支付失败控制器中 重新激活购物车,并删除支付方式和保留订单ID并重复使用它;


/**
* 重写MagentoCheckoutControllerOnepageFailure
*/

public function execute()
{
    $lastQuoteId = $this->getOnepage()->getCheckout()->getLastQuoteId();
    $lastOrderId = $this->getOnepage()->getCheckout()->getLastOrderId();    
    if (!$lastQuoteId || !$lastOrderId) {
        return $this->resultRedirectFactory->create()->setPath('checkout/cart');    
    }
    $this->addOldCart();    
    return $this->resultPageFactory->create();
}

protected function addOldCart()
{
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();   
     $quote = $objectManager->create('MagentoQuoteModelQuoteFactory')->create()->load($this->getOnepage()->getCheckout()->getLastQuoteId());    
     $this->_registry->register('reserved_order_id', $quote->getReservedOrderId());    
     $quote->setReservedOrderId(null);    
     $quote->setIsActive(true);    
     $quote->removePayment();    
     $quote->save();    
     $this->checkoutSession->replaceQuote($quote);    
     $this->cart->setQuote($quote);    
     $this->checkoutSession->restoreQuote();
}

在FPC缓存开启的情况下还原购物车可能失败,需要禁用FPC缓存

0