JS functions which subscribe to shippingService.getShippingRates() are called twice
app/code/Magento/Checkout/view/frontend/web/js/view/cart/totals.js between l.24 and l.25A clue to resolve this problem is to remove in the file app/code/Magento/Checkout/view/frontend/web/js/model/shipping-service.js, the line shippingRates.valueHasMutated() (l.24), because the l.23 already call the subscribed functions.
Observed behavior is correct.
Suggested change won't work in case rates array structure is the same with different contents.
See https://forums.asp.net/t/2056128.aspx?What+is+the+use+of+valueHasMutated+in+Knockout+js and https://stackoverflow.com/questions/13316034/knockoutjs-calling-valuehasmutated-too-often?noredirect=1&lq=1 for more details.
I have only one problematic case if I remove the line. It is when I push an element directly in my array
this.shippingRates().push(myCustomShippingRate);
With that and without the shippingRates.valueHasMutated(), my subscribers are not notified.
But, I'm a good developper, so when I change an element in my array, I set it in my object after :
this.shippingRates().push(myCustomShippingRate);
shippingService.setShippingRates(this.shippingRates());
My test is made inside the app/code/Magento/Checkout/view/frontend/web/js/view/cart/shipping-rates.js in order to be able to manually add a shippingRate.
With consecutive calls to shippingService.setShippingRates(data); will subscribers be notified in case only one element of array is changed?
I don't know excatly what you mean but if I do :
this.shippingRates().push(duplicated);
shippingService.setShippingRates(this.shippingRates());
shippingService.setShippingRates(this.shippingRates());
shippingService.setShippingRates(this.shippingRates());
My subscribers are called 3 times with my new array element inside :

2 elements before
3 elements after (displayed 3 times)
@maximehuran the same amount of elements but change some of them I mean.
I can easily change the carrier_code of an element and set it just after :
this.shippingRates()[0].carrier_code = 'hello';
shippingService.setShippingRates(this.shippingRates());
Log after change :

3 elements after (displayed 3 times)
Sorry, missed this earlier. As we assume shippingService.setShippingRates should always be used and not just direct modifications of previously set array, it is safe to remove this line.
Could you prepare a pull request against 2.2-develop branch please?
@maximehuran, thank you for your report.
We've acknowledged the issue and added to our backlog.
Hi @maximehuran was this issue fixed with the pull request https://github.com/magento/magento2/pull/14314 I see a comment on you pull request linking to this one.
@dmanners did you read comments in PR? It seems to be reverted afterwards.
Hi @orlangur nope I did not ready all the comments at the end. GitHub does not do a great job of showing reverted PRs that have not been reverted via github itself
@dmanners yep, just fyi, I didn't even check whether it was really reverted.
Hi @engcom-Alfa. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.3-develop branchDetails
- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
- If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
- If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
Hi @engcom-Echo. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX label(s) to the ticket, indicating the components it may be related to.[ ] 2. Verify that the issue is reproducible on 2.4-develop branchDetails
- 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_!
[ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.
:white_check_mark: Confirmed by @engcom-Echo
Thank you for verifying the issue. Based on the provided information internal tickets MC-30132 were created
Issue Available: @engcom-Echo, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Thank you for your contributions.
Most helpful comment
Sorry, missed this earlier. As we assume
shippingService.setShippingRatesshould always be used and not just direct modifications of previously set array, it is safe to remove this line.Could you prepare a pull request against
2.2-developbranch please?