Magento 2: Как восстановить корзину после отмены/неудачной оплаты?
Я использовал следующий код, но он не работает, я все равно получаю пустую корзину.
$comment = __('Payment has been canceled.');
if ($order->getId() && $order->getState() != Order::STATE_CANCELED) {
$order->registerCancellation($comment)->save();
}
$session->restoreQuote();
$this->messageManager->addNoticeMessage($comment);
$this->_redirect('checkout/cart');
1
1 answers
Проверьте это, оно работает для меня в Magento 2.4: (вдохновленный этим ответом)
<?php
namespace Vendor\Extension\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Checkout\Model\Session;
use Magento\Quote\Model\QuoteFactory;
class MyAction extends Action
{
public function __construct(
Context $context,
Session $checkoutSession,
QuoteFactory $quoteFactory
) {
parent::__construct($context);
$this->checkoutSession = $checkoutSession;
$this->quoteFactory = $quoteFactory;
}
public function execute()
{
$id = $this->checkoutSession->getLastQuoteId();
$quote = $this->quoteFactory->create()->loadByIdWithoutStore($id);
if (!$quote->getId()) {
// enter your code on fail (if quote not found) here.
// action method must return Result object, not boolean.
return false;
}
$quote->setIsActive(true)->setReservedOrderId(null)->save();
$this->checkoutSession->replaceQuote($quote);
// action method must return Result object, not boolean.
// write your redirect, JSON, or other result here.
return true;
}
}
0
Author: Iaroslav Glodov, 2020-11-18 10:21:43