Magento 2 – Refresh Quote Data in Checkout Frontend

checkoutknockoutjsmagento2tax

Background

I am working on a module that replaces the shipping address, shipping method, and payment steps during checkout with an external iframe solution. This is basically a replacement for checkout, however I am trying to do it in a way that is minimalistic in what I rewrite (why re-invent the wheel for things I don't really need to re-invent?) When address information is updated in the iframe, a call-back POST hits a custom controller where I update the address and shipping method on the quote.

Question

  • Is there a way I can tell the frontend that it needs to refresh its copy of the data with what is actually in the database?

Things I've tried already

  • I have tried firing off the appropriate JS functions to set the billing/shipping address and shipping method on the quote. In fact, I am doing this now as it seemed to be the only way to get the shipping subtotal to populate. However, this has problems of its own:
    • Since I don't know the correct region_id, I either have to do custom AJAX calls to look it up or load some massive JS object to iterate over looking for the appropriate ID (I'm currently doing the AJAX thing as it seemed less intesive on the browser)
    • There are race conditions that occur that cause address changes to get overwritten by the previous address (not to mention all the extra DB traffic that is happening because of this)
    • Even though sales tax has been calculated correctly in the database, it is still not showing in the subtotals or grand total on the frontend (this is actually the real problem I am trying to solve here, however I feel the current setup I have is overly complicated and would be cleaner if Magento would just refresh its data from the database)
  • I have tried loadSection calls. But those seem to only update the mini-cart or the sidebar totals

Things I cannot change

  • I cannot change the callback POSTs (that is, I can't stop them from happening). They must occur in order for the iframe to get updated information from Magento (eg, sales tax, shipping rates, etc..)
  • For reasons I won't go into here, I need to reuse as much of the existing checkout as possible (see background section where I mentioned trying to be as minimalistic as possible)
  • I cannot change anything on the form inside the iframe (eg, I can't pass it region ids to include in the region/state dropdown). It is intentionally generic as it is used with other ecommerce platforms.

Best Answer

I believe I have solved this. Here is what I did:

I added Magento_Checkout/js/action/get-totals to the define section of my JS file. This allowed me to call getTotals([]) to get the sidebar to update after the callback posts occur (I happen to have JS events that fire letting me know the callbacks have been triggered)

After all this, I was able to remove all of my other JS updates to set billing/shipping addresses and shipping method as these were no longer needed.

Related Topic