magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

[Issue] Wrong store ID used in Order Address Renderer

Open m2-assistant[bot] opened this issue 1 year ago • 4 comments

This issue is automatically created based on existing pull request: magento/magento2#37932: Wrong store ID used in Order Address Renderer


Description (*)

\Magento\Sales\Model\Order\Address\Renderer::format allows to format order address. Internally, the method retrieve the order store ID and update the current store ID. This produces strange behavior if used in a context with a different store as the current store is not set to its previous value at the end of the execution of the method.

The class \Magento\Customer\Model\Address\Config is used within the format method in order to resolve the address format to use accordingly to the order store. This class allows to set the store context thanks to \Magento\Customer\Model\Address\Config::setStore. However this method was not called. Here we call it and reverse the value to its original state at the end of the execution, in order to prevent weird behavior. With this solution, the current store in the manager is not altered and the global context is preserved. Previously, when the method \Magento\Customer\Model\Address\Config::getFormatByCode was called, by the method format, the store was resolved at the first call :

-> \Magento\Customer\Model\Address\Config::getFormatByCode
--> \Magento\Customer\Model\Address\Config::getFormats
---> \Magento\Customer\Model\Address\Config::getStore
public function getStore()
{
    if ($this->_store === null) {
        $this->_store = $this->_storeManager->getStore();
    }
    return $this->_store;
}

I guess that's why there was a $this->storeManager->setCurrentStore($orderStore); in \Magento\Sales\Model\Order\Address\Renderer::format.

This MR aims to remove the usage of $this->storeManager->setStore which may produce unwanted side-effects and use proper method available in the \Magento\Customer\Model\Address\Config API.

Related Pull Requests

Fixed Issues (if relevant)

Cases where multiple order mails (with different stores) have to be sent by the cron job. Order addresses are not formatted accordingly to their respective store ID. Different formats must be set by store ID.

Manual testing scenarios (*)

  1. ...
  2. ...

Questions or comments

Contribution checklist (*)

  • [ ] Pull request has a meaningful description of its purpose
  • [ ] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

m2-assistant[bot] avatar Feb 02 '24 05:02 m2-assistant[bot]

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:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.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.

m2-assistant[bot] avatar Feb 07 '24 08:02 m2-assistant[bot]

Hello @thomas-kl1,

Thank you for the report and collaboration!

Verified this issue on 2.4-develop. As described, when \Magento\Sales\Model\Order\Address\Renderer::format function is invoked, it will change the current store to the store in which the order was placed, after this it should be reverted to the original store id. But the store id is not reverted to original id.

Please take a look at the screenshot: image

Please find the attached module used to reproduce this issue. OrderVendor.zip

Thank you.

engcom-November avatar Feb 07 '24 09:02 engcom-November

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

github-jira-sync-bot avatar Feb 07 '24 09:02 github-jira-sync-bot

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

m2-assistant[bot] avatar Feb 07 '24 09:02 m2-assistant[bot]