magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.54k stars 9.32k forks source link

about Downloadable Product Zero Subtotal Checkout Issue... #25177

Closed mugua closed 5 months ago

mugua commented 5 years ago

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"
m2-assistant[bot] commented 5 years ago

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

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?


mugua commented 5 years ago

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?

m2-assistant[bot] commented 5 years ago

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:

mugua commented 5 years ago

any feedback?

m2-assistant[bot] commented 4 years ago

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:

magento-engcom-team commented 4 years ago

: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.

plastikschnitzer commented 4 years ago

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!

plastikschnitzer commented 4 years ago

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

trunglv commented 4 years ago

Quick fixing :

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

}

j-robin-hunter commented 4 years ago

I also have this same issue on 2.3.4

mugua commented 4 years ago

Quick fixing :

  • 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>

-- 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 ?

mugua commented 4 years ago

@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 ?

j-robin-hunter commented 4 years ago

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

trunglv commented 4 years ago

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 Magento\Sales\Model\ResourceModel\Order\Handler\State. .... 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 :

j-robin-hunter commented 4 years ago

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.

mugua commented 4 years ago

@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...

plastikschnitzer commented 4 years ago

@magento give me 2.4-develop instance

magento-engcom-team commented 4 years ago

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

magento-engcom-team commented 4 years ago

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.

plastikschnitzer commented 4 years ago

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

KineoMichaelT commented 4 years ago

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.

maynard321 commented 4 years ago

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));
    }
}
curkle commented 4 years ago
 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

magento-quality-patches commented 4 years ago

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

plastikschnitzer commented 3 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 3 years ago

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

magento-deployment-service[bot] commented 3 years ago

Hi @plastikschnitzer, here is your Magento Instance: https://a7a1528cbe77bbf393e0d07b27c5b1cd-2-4-develop.instances.magento-community.engineering Admin access: https://a7a1528cbe77bbf393e0d07b27c5b1cd-2-4-develop.instances.magento-community.engineering/admin_9dba Login: a2f94bde Password: 739b10bc8e97

github-jira-sync-bot commented 3 years ago

:white_check_mark: Jira issue https://jira.corp.magento.com/browse/AC-1035 is successfully created for this GitHub issue.

m2-assistant[bot] commented 3 years ago

:white_check_mark: Confirmed by @engcom-Alfa. Thank you for verifying the issue.
Issue Available: @engcom-Alfa, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

engcom-Bravo commented 5 months ago

Hi @mugua,

Thanks for your reporting and collaboration.

We have verified the issue in 2.4-develop instance and the issue is no more reproducible.Kindly refer the attached screenshot.

Steps to reproduce

Screenshot 2024-05-17 at 13 10 31 Screenshot 2024-05-17 at 13 11 06

User Order status should be "Complete" andUser Downloadable should be "Available".

Hence We are closing this issue.

Thanks.