Order status is closed if a customer purchased a free downloadable product.

The issue was posted to Magento2 Github

https://github.com/magento/magento2/issues/25177

My solution :

  • Reference Class: Magento\Sales\Model\ResourceModel\Order\Handler\State
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <preference for="Magento\Sales\Model\ResourceModel\Order\Handler\State" type="FixBugs\VirtualOrderStatus\Sales\Model\ResourceModel\Order\Handler\State" />
</config>

— To adjust: If grand-total > 0 so don’t set order status to be CLOSED

<?php

namespace FixBugs\VirtualOrderStatus\Sales\Model\ResourceModel\Order\Handler;

use Magento\Sales\Model\Order;

class State extends \Magento\Sales\Model\ResourceModel\Order\Handler\State {
    
    public function check(Order $order)
    {
        
        $currentState = $order->getState();
        if ($currentState == Order::STATE_NEW && $order->getIsInProcess()) {
            $order->setState(Order::STATE_PROCESSING)
                ->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING));
            $currentState = Order::STATE_PROCESSING;
        }

        if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice()) {
            if (in_array($currentState, [Order::STATE_PROCESSING, Order::STATE_COMPLETE])
                && !$order->canCreditmemo()
                && !$order->canShip()
                && $order->getGrandTotal() > 0        
            ) {
                $order->setState(Order::STATE_CLOSED)
                    ->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_CLOSED));
                
            } elseif ($currentState === Order::STATE_PROCESSING && !$order->canShip()) {
                $order->setState(Order::STATE_COMPLETE)
                    ->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_COMPLETE));
            }
        }
        return $this;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *