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

Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface add an extension attributes that can be used for calculating totals #36781

Open leonhelmus opened 1 year ago

leonhelmus commented 1 year ago

Description

When using the api /rest/V1/order/:order_id/refund you are able to add arguments that can be used when creating creditmemos.

The only problem is that these extension attributes cannot be added to creditmemo without using a preference. Which is a shame i would suggest changing the method: https://github.com/magento/magento2/blob/8d6519b082be71578561ee0fc2d4eab25b9a6232/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L213 To a public function. This way third parties can add a after plugin and do not have to use a preference. Which makes it easier to pass an custom argument with extension attributes from interface Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface to interface Magento\Sales\Api\Data\CreditmemoInterface

This way will than work for both: https://github.com/magento/magento2/blob/8d6519b082be71578561ee0fc2d4eab25b9a6232/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L78 as well as for: https://github.com/magento/magento2/blob/8d6519b082be71578561ee0fc2d4eab25b9a6232/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L110

Expected behavior

You add a custom extension attribute like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\CreditmemoCreationArgumentsInterface">
        <attribute code="test" type="boolean"/>
    </extension_attributes>

    <extension_attributes for="Magento\Sales\Api\Data\CreditmemoInterface">
        <attribute code="test" type="boolean"/>
    </extension_attributes>
</config>

After you add a plugin to, since the method is public: https://github.com/magento/magento2/blob/8d6519b082be71578561ee0fc2d4eab25b9a6232/app/code/Magento/Sales/Model/Order/CreditmemoFactory.php#L213

    public function afterInitData($creditmemo, $data)
    {
        if (isset($data['extension_attributes']['test'])) {
            $extensionAttributes = $creditmemo->getExtensionAttributes();
            $extensionAttributes->setTest(
                $data['extension_attributes']['test']
            );
        }
    }

After you use this endpoint: rest/V1/order/:order_id/refund with body:

{
    "items": [
        {
            "extension_attributes": { },
            "order_item_id": <order_item_id>,
            "qty": 1
        }
    ],
    "arguments": {
        "extension_attributes": {
            "test": true
        }
    }
}

The extension attribute can be used when calculating the totals if you would like to customize totals.

Benefits

If we make it public multiple vendors can add an after plugin to add the data they need on the creditmemo. Now only one vendor can make a preference on this function, because it's protected.

Additional information

No response

Release note

No response

m2-assistant[bot] commented 1 year ago

Hi @leonhelmus. Thank you for your report. To speed up processing of this issue, 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.