1

Need possible use cases for a scenario in magento 2

when a customer places an order during checkout after entering place order stock from inventory gets reduced then customer moved to payment gateway for payment, but in case if a customer is not paying, the stock reduced remains the same until admin manually cancels the order from backend, I need use cases to alter the backend to remain the stock no mater customer pays or not and reduce it once the order is completely processed.

Thanks in advance

1 Answer 1

1

Try this all steps

1.app/code/Custom/Module/registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_Module',
    __DIR__
);

2.app/code/Custom/Module/etc/module.xml:

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Custom_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
  1. Create a custom order status Order to track the order status, create a custom order status called "Pending Payment" in the Magento backend

  2. Override the order cancellation process.

    Magento\Sales\Controller\Adminhtml\Order class to restore the stock when an order is canceled from the backend.

  3. Create this files.

app/code/Custom/Module/etc/adminhtml/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Controller\Adminhtml\Order\Cancel" type="Custom\Module\Controller\Adminhtml\Order\Cancel"/>
</config>

6.app/code/Custom/Module/Controller/Adminhtml/Order/Cancel.php:

 <?php
namespace Custom\Module\Controller\Adminhtml\Order;

class Cancel extends \Magento\Sales\Controller\Adminhtml\Order\Cancel
{
    public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        $orderId = $this->getRequest()->getParam('order_id');
        if ($orderId) {
            try {
                $order = $this->orderRepository->get($orderId);
                if ($order->getStatus() !== \Magento\Sales\Model\Order::STATE_CANCELED) {
                    $this->orderManagement->cancel($orderId);
                    // Restore stock for the canceled order
                    $this->restoreStock($order);
                    $this->messageManager->addSuccessMessage(__('The order has been canceled.'));
                } else {
                    $this->messageManager->addErrorMessage(__('The order has already been canceled.'));
                }
            } catch (\Exception $e) {
                $this->messageManager->addErrorMessage(__('Could not cancel the order: %1', $e->getMessage()));
            }
        }
       

Hope you this is useful to you

1
  • am sorry I think my question was quiet confusing I just want my stock to be updated only when payment is complete instead of while placing the order before payment step
    – Rekha S
    Commented May 30, 2023 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.