Magento2: about Downloadable Product Zero Subtotal Checkout Issue...

Created on 21 Oct 2019  ·  24Comments  ·  Source: magento/magento2

Preconditions (*)

  1. Magento 2.3.3 & 2.3-develop
  2. PHP 7.3.10

Steps to reproduce (*)

  1. Create a Zero Downloadable Product, set it can be download “INVOICED”
  2. set Zero Subtotal Checkout Order status Processing and Auto INVOICE

Expected result (*)

  1. User Order status should be "Complete"
  2. User Downloadable should be "Aviable"

Actual result (*)

  1. User Order status is "Closed"
  2. User Downloadable is "Pending"
Checkout Payment Confirmed P2 ready for dev Reproduced on 2.3.x S2

Most helpful comment

Quick fixing :

  • Reference Class: MagentoSalesModelResourceModelOrderHandlerState
<?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>

-- 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;
    }
}

All 24 comments

Hi @mugua. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@mugua do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

1: User placed order sucessed, and choose the Zero Subtotal Checkout payment, It should be complete, why I got a Closed status?
2: the Downloadable product has invoice, why it still stay in Pending status?

Hi @sudheers-kensium. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

any feedback?

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-23220 were created

Issue Available: @engcom-Charlie, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

I ran into the same issue on Magento 2.3.4
Any hotfix available?

This issue cancelled my Magento 2 migration for the moment, so any quick fix would be highly appreciated!

I could reproduce the issue on a vanilla Magento 2.4-dev instance here: https://github.com/magento/magento2/issues/27590#issuecomment-609459330

Quick fixing :

  • Reference Class: MagentoSalesModelResourceModelOrderHandlerState
<?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>

-- 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;
    }
}

I also have this same issue on 2.3.4

Quick fixing :

  • Reference Class: MagentoSalesModelResourceModelOrderHandlerState
<?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>

-- 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;
    }
}

Thanks for your fix, it is fine for me now, the magento official still haven't give solution to solve this issue,
@magento-engcom-team, any better fix method for this issue ?

@trunglv just check the code you offered, it means if grand total>0,the po status shoud be closed. If grand total <=0, the po status will be complete.
does this influence other POs grand total >0?
Modified to
&& $order->getGrandTotal() < 0
Maybe better ?

This fix will not work if you are wanting to create a 'free' virtual product, for example the opportunity to download a demo or test product

Hi @j-robin-hunter , @mugua
My use case for my fix: A customer purchases a free downloadable product but he can't download a file due to Order Status is Closed.
It depends on your use cases, so maybe you will make different changes by rewriting MagentoSalesModelResourceModelOrderHandlerState. ....
Anyway, another solution, maybe better, is to check whether an order is "Zero Subtotal Checkout Payment", If yes, we will prevent order status to be CLODED

if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice()) {
            if (in_array($currentState, [Order::STATE_PROCESSING, Order::STATE_COMPLETE])
                && !$order->canCreditmemo()
                && !$order->canShip()
                && $order->getPayment()->getMethod() != 'free'      
            ) {
                $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));
            }
        }

@j-robin-hunter : I think you can set configuration values of Zero Subtotal Checkout payment :

  • New Order Status => Processing
  • Automatically Invoice All Items => Yes
    Maybe it will help.

This fix would work for our use case. However, I do wonder if this state check is a good idea as it clearly able to 'break' the documented order process workflow which would only allow the status to be closed if a refund is issued. This check clearly makes decisions to close the order on whether an item can be shipped.

For completeness, we do have the order status set to processing and automatically invoice all items so provided the order status is not set to closed this ought to allow the workflow to continue.

@trunglv @j-robin-hunter If a virtual product or a downloadable product's price is >0 , they both don't need Creditmemo & Ship, if using the code "&& $order->getPayment()->getMethod() != 'free'". it still goto closed status again... so i think "&& $order->getGrandTotal() < 0" is better than the last fix method, BTW, it is still the hotfix now, we can wait for the magento official fix method soon...

@magento give me 2.4-develop instance

Hi @plastikschnitzer. Thank you for your request. I'm working on Magento 2.4-develop instance for you

Hi @plastikschnitzer, here is your Magento instance.
Admin access: https://i-25177-2-4-develop.instances.magento-community.engineering/admin_6a5d
Login: fed670cf Password: d8a2e579f19c
Instance will be terminated in up to 3 hours.

Hi @magento I can confirm the issue still exists on the latest 2-4-dev instance.

Still an issue from my testing, so adding the details from my investigation to see if it's helpful.

@j-robin-hunter The check location for the fix should resolve the issue, but it's the opposite of your concern. It's the documented workflow that is currently broken. Ultimately, the error is coming from Magento/Sales/Order::canCreditmemoForZeroTotal in which it determines if the amount refunded = amount paid, it must be fully refunded, not taking to account that for zero total checkouts 0=0 and isn't really refunded. That error propagates up to the State check as $order->canCreditmemo() returns false when otherwise it should be returning true.

    private function canCreditmemoForZeroTotal($totalRefunded)
    {
        $totalPaid = $this->getTotalPaid();
        //check if total paid is less than grandtotal
        $checkAmtTotalPaid = $totalPaid <= $this->getGrandTotal();
        //case when amount is due for invoice
        $hasDueAmount = $this->canInvoice() && ($checkAmtTotalPaid);
        //case when paid amount is refunded and order has creditmemo created
        $creditmemos = ($this->getCreditmemosCollection() === false) ?
             true : (count($this->getCreditmemosCollection()) > 0);
        $paidAmtIsRefunded = $this->getTotalRefunded() == $totalPaid && $creditmemos;
        if (($hasDueAmount || $paidAmtIsRefunded) ||
            (!$checkAmtTotalPaid &&
            abs($totalRefunded - $this->getAdjustmentNegative()) < .0001)) {
            return false;
        }
        return true;
    }

I don't know enough about the other implications of changing this at the source though, so trunglv's fix is the safest without a magento developer taking a look.

New to this, so forgive me if I'm barking up the wrong tree.
It seems that that error comes in the logic of checking !$order->canShip() - this whole section is asking "is something wrong", and "it can't ship" is being thought of as "yes, something is wrong". However, the test "isVirtual" is included in the canShip testing, and while every other part of canShip is "something is wrong", isVirtual is not wrong.
So I've changed that line to && (!$order->canShip() && !$order->getIsVirtual()) and it seems to work:

if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice()) {
    if (in_array($currentState, [Order::STATE_PROCESSING, Order::STATE_COMPLETE])
        && !$order->canCreditmemo()
        && (!$order->canShip() && !$order->getIsVirtual())
     ) {
        $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));
    }
}
 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 || $this->isFreePaymentFullyRefunded($order)) // check if it is free payment and also all refunded
            ) {
                $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;
    }

    private function isFreePaymentFullyRefunded(Order $order)
    {
        $totalInvoicedItem = 0;
        $totalRefundItem = 0;

        foreach ($order->getItems() as $item) {
            $totalInvoicedItem += $item->getQtyInvoiced();
            $totalRefundItem  += $item->getQtyRefunded();
        }

        return $totalRefundItem > 0 && $totalRefundItem == $totalInvoicedItem;

    }

add function check if all item has been refund. it should work

The patch solving a similar issue is available in Magento Quality Patches package (MQP)

Patch
MDVA-28656: Fixes the issue where if an order was placed with no payment information required (for example, with 100% discount) and an invoice was created for the order, the order status changes to Closed instead of Complete.

Compatible versions
Magento OpenSource/Commerce/Commerce Cloud 2.3.1 - 2.3.5, 2.4.0

:warning: We strongly recommend testing all patches in a staging or development environment before deploying to production.

Applying a patch - Magento OpenSource/Commerce

  1. $ composer require magento/quality-patches
  2. $ ./vendor/bin/magento-patches apply MDVA-28656

See MQP Magento Commerce documentation

Applying a patch - Magento Commerce Cloud
See MQP Magento Commerce Cloud documentation

Was this page helpful?
0 / 5 - 0 ratings