Description
Preconditions (*)
- Magento 2.3.3 & 2.4-develop.
Steps to reproduce (*)
- This problem only appears when M2 is extended and UPS shipping set up
- I have an installation where 3 different modules fetch the shipping totals on the cart page
- They do this via:
Magento\Quote\Model\Quote\TotalsCollector->collectAddressTotals()
which then executes:
Magento\Shipping\Model\Rate->getAllRates()
which then executes:
Magento\Ups\Model\Carrier->_getXmlQuotes()
The problem is now that _getXmlQuotes
does a remote request to: https://onlinetools.ups.com/ups.app/xml/Rate
which takes 2-3s per request (see: vendor/magento/module-ups/Model/Carrier.php
line 782).
The problem is that the remote request to the UPS API is not cached. So in my case this is executed 3 times for each page load of the cart page. Additionally this is worsened by the fact that the cart page does 2 more API calls to:
rest/default/V1/guest-carts/[token]/totals-information (see: vendor/magento/module-checkout/etc/webapi.xml)
rest/default/V1/guest-carts/[token]/estimate-shipping-methods (see: vendor/magento/module-quote/etc/webapi.xml)
which trigger the same extensions again resulting in 3 additional calls each. So in total for each page load of the cart page 9 identical requests to the UPS API are sent.
In case somebody else runs into this. In my case the initiating modules are:
vendor/amasty/promo/Plugin/Quote/Model/Quote/TotalsCollectorPlugin.php 119
vendor/aheadworks/module-sarp2/Model/Shipping/RatesCollector.php 77
vendor/mirasvit/module-rewards/src/Rewards/Model/Total/Quote/Discount.php 259
Additionally:
While looking at vendor/magento/module-ups/Model/Carrier.php I noticed that the XML for the remote request is manually pieced together. The parameters inserted into the XML are not escaped. If any of the parameters contains i.e. '<' the entire request falls apart. Simplexml is a system requirement for M2 and the XML should be generated using a XML lib.
Expected result (*)
- Remote request is cached.
Actual result (*)
- Remote request gets executed multiple times