Posted on

Magento 2 Restore Quote/Cart after order is cancelled/Failed

I am Trying to Restore Quote/Cart after order is cancelled/Failed in custom payment gateway (CCAvenue) but the problem it the quantity of item restored is doubled.

protected function _cancelPayment($errorMsg = ”)
{
$errorMsg = trim(strip_tags($errorMsg));

$gotoSection = false;
$this->_checkoutHelper->cancelCurrentOrder($errorMsg);
if ($this->_checkoutSession->restoreQuote()) {
//Redirect to payment step
$gotoSection = ‘paymentMethod’;
}

return $gotoSection;
}

in above code $this->_checkoutHelper->cancelCurrentOrder($errorMsg); is use to cancel last real order and $this->_checkoutSession->restoreQuote() is use to restore the cart but the problem is both the statements increase quantity of product.

Eg.

suppose I have Product XYZ with stock quantity: 1

When I add product to cart and Checkout The order is placed and stock is reduced to 0.

but when I click cancel on Payment gateway I return Back to my site and the above method is called.
And now the stock increase to 2 because $this->_checkoutHelper->cancelCurrentOrder($errorMsg); and $this->_checkoutSession->restoreQuote() increase the quantity of the product.

Is there any way to do both cancel order and restore quote while restore stock only once.

Edit: Complete Controller File.

<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MagentoCcavenuepayController;
use MagentoCcavenuepayHelperData;
/**
* Payflow Checkout Controller
*/
abstract class Ccavenuepay extends MagentoFrameworkAppActionAction
{
/**
* @var MagentoCheckoutModelSession
*/
protected $_checkoutSession;

/**
* @var MagentoSalesModelOrderFactory
*/
protected $_orderFactory;

/**
* @var PsrLogLoggerInterface
*/
protected $_logger;
protected $logger;

/**
* @var MagentoCcavenuepayModelCcavenuepay
*/
protected $_ccavenuepay;

/**
* @var MagentoCcavenuepayHelperCheckout
*/
protected $_checkoutHelper;

/**
* @var MagentoCcavenuepayHelperdata
*/
protected $_ccavenuepayHelper;

/**
* Redirect block name
* @var string
*/
protected $_redirectBlockName = ‘ccavenuepay.iframe’;

/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoCheckoutModelSession $checkoutSession
* @param MagentoSalesModelOrderFactory $orderFactory
* @param MagentoCcavenuepayModelPayflowlinkFactory $ccavenuepay
* @param MagentoCcavenuepayHelperCheckout $checkoutHelper
* @param PsrLogLoggerInterface $logger
*/

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoCheckoutModelSession $checkoutSession,
MagentoSalesModelOrderFactory $orderFactory,
MagentoCcavenuepayModelCcavenuepay $ccavenuepay,
MagentoCcavenuepayHelperCheckout $checkoutHelper,
PsrLogLoggerInterface $logger
) {

$writer = new ZendLogWriterStream(BP . ‘/var/log/test.log’);
$this->logger = new ZendLogLogger();
$this->logger->addWriter($writer);
$this->logger->info(“__construct=====Ccavenuepay extends MagentoFrameworkAppActionAction”);

$this->_checkoutSession = $checkoutSession;
$this->_orderFactory = $orderFactory;
$this->_logger = $logger;
$this->_ccavenuepay = $ccavenuepay;
$this->_checkoutHelper = $checkoutHelper;
//$this->_ccavenuepayHelper = $this->_ccavenuepay;
parent::__construct($context);
}

/**
* Cancel order, return quote to customer
*
* @param string $errorMsg
* @return false|string
*/
protected function _cancelPayment($errorMsg = ”)
{
$errorMsg = trim(strip_tags($errorMsg));

$gotoSection = false;
$this->_checkoutHelper->cancelCurrentOrder($errorMsg);
if ($this->_checkoutSession->restoreQuote()) {
//Redirect to payment step
$gotoSection = ‘paymentMethod’;
}

return $gotoSection;
}

public function errorAction()
{
$this->_redirect(‘checkout/onepage/’);
}

//check_module_upload
//newmoduleupdate_now
/**
* Check if email is registered at Moneybookers
*/

protected function _getCheckout()
{
return $this->_objectManager->get(‘MagentoCheckoutModelSession’);
}
/**
* Get session model
*
* @return MagentoCcavenuepayModelCcavenuepaySession
*/
protected function _getCcavenuepayPostSession()
{
return $this->_objectManager->get(‘MagentoCcavenuepayModelCcavenuepaySession’);
}
protected function _getHelper()
{
return $this->_ccavenuepay->getHelper();
}
}

Leave a Reply

Your email address will not be published.