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.48k stars 9.29k forks source link

Creditmemo Gift Cards (Partial Refund) #36559

Closed zepgram closed 1 year ago

zepgram commented 1 year ago

Preconditions and environment

Steps to reproduce

  1. Add 1 gift card product to the cart
  2. Add 1 simple product or any other types
  3. Place an order
  4. Navigate to order detail in admin panel
  5. Invoice the order
  6. Navigate to invoce detail
  7. Click Creditmemo
  8. Create a Creditmemo for the the simple product

Expected result

  1. The gift card account should be kept

Actual result

  1. The gift card account is deleted

Additional information

Take a look here, the reason is very simple magento/vendor/magento/module-gift-card-account/Model/Plugin/CreditmemoRepository.php

 /**
 * Remove gift card account for refund
 *
 * @param CreditmemoRepositoryInterface $subject
 * @param CreditmemoInterface $entity
 *
 * @return CreditmemoInterface
 *
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function afterSave(CreditmemoRepositoryInterface $subject, CreditmemoInterface $entity): CreditmemoInterface
{
    $order = ($entity->getOrder()) ?: $this->getOrderById((int)$entity->getOrderId());
    $orderItems = $this->getOrderItems($order);
    if ($orderItems) {
        foreach ($entity->getItems() as $creditMemoItem) {
            $orderItem = $orderItems[$creditMemoItem->getOrderItemId()];
            if ($orderItem->getProductOptionByCode('giftcard_created_codes')) {
                $this->removeGiftCardAccount(
                    $orderItem->getProductOptionByCode('giftcard_created_codes'),
                    abs((int)$creditMemoItem->getQty())
                );
            }
        }
    }

    return $entity;
}

The error is here:

 foreach ($entity->getItems() as $creditMemoItem) {

Should be:

 foreach ($entity->getAllItems() as $creditMemoItem) {

This way, we exclude the gift card item who has been previously deleted from the object (because she's not targeted by our creditmemo)

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 year ago

Hi @zepgram. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

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

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 1 year ago

Hi @engcom-November. 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:

engcom-November commented 1 year ago

Hi @zepgram , Thank you for reporting and collaboration. Verified the issue on Magento 2.4-develop instance with enterprise edition modules enabled but the issue is not reproducible with below steps performed. Steps performed: Placed an order from front end with gift card product and simple product. Created invoice from admin and credit memo for only simple product. No issue. Credit memo / refund created for only simple product as expected. No gift card account is deleted. Kindly recheck the issue on Magento 2.4-develop instance with enterprise edition and provide missing steps if any if the issue is still reproducible. Thank you.

zepgram commented 1 year ago

You must also invoice the gift card product before creating the creditmemo for the simple product

engcom-November commented 1 year ago

Hi @zepgram , invoice has been created for gift-card product as well but credit memo created only for simple product.

zepgram commented 1 year ago

I don't know how you do, anytime I'm reporing something you cannot reproduce. Just look at the code, it's wrong anyway.

engcom-November commented 1 year ago

Hi @zepgram , I verified the code in Magento 2.4-develop instance and the code is the same which mentioned by you but still unable to reproduce the issue. I rechecked again with Gift card type Virtual and Combination but could not able to reproduce the issue. Please find the below screenshots of the steps performed: Steps performed:

  1. Admin - Stores - Configuration - Sales - Gift cards - Created gift card
  2. Created Gift card product of Virtual type with open amount selected.
  3. Login to Front end and added simple product and gift card product to cart
  4. Proceed to checkout and paid with Gift card balance
  5. Admin - Sales - Orders - Created invoice for both products
  6. Credit memo created only for simple product. No issue: If refund to store credit is selected - amount credited to store credit balance Gift card account is not deleted. Balance deducted as expected. image image Kindly let us know if you are using any third party extensions / modules and recheck on clean 2.4-develop enterprise edition instance, provide missing steps if any if the issue is still reproducible. Thank you.
zepgram commented 1 year ago

Then you must probably disable the store credit to reproduce

engcom-November commented 1 year ago

Hi @zepgram , Thank you for the response. Verified the issue again on latest Magento 2.4-develop instance with enterprise edition modules enabled with below steps but the issue is still not reproducible. Steps performed:

  1. Disabled Store credit option from Admin (Stores - Settings - Configuration - Customers - Customer Configuration - Enable Store credit - No) and save
  2. Reindex and clear cache
  3. Created Gift card account from - Marketing - Promotions - Gift Card Accounts
  4. Created Gift card product and simple product
  5. Front end - Added both the products to cart
  6. Placed order using Gift card balance
  7. Admin - Order details page - Created invoice for both the products
  8. Credit Memo - Update Quantity 0 for gift card product and create credit memo only for simple product. No issue: Credit memo created successfully and balance updated as expected in Gift card account. Gift card account is not deleted. Can you please check if the issue occurred due to any 3rd party extensions / modules enabled? Kindly recheck the issue on Magento 2.4-develop instance as it is having latest code base and provide missing steps if any if the issue is still reproducible. Thank you. image image image
engcom-November commented 1 year ago

Hi @zepgram , We are closing this issue as there has been no latest update on the same. Kindly reopen / create new issue if you are still facing any issues. Thank you.

zepgram commented 1 year ago

Quality patch available here: https://experienceleague.adobe.com/docs/commerce-knowledge-base/kb/support-tools/patches/v1-1-32/acsd-51230-gift-card-account-is-deleted.html?lang=en

https://github.com/magento/quality-patches/blob/master/patches/commerce/ACSD-51230_2.4.3-p1.patch

Looks like this is reproducible :1st_place_medal: