I am building a food ordering website using WordPress and Woocommerce. So far I have been doing good but got stuck at one point where the Shipping method should be chosen automatically.
What is the flow like? The user visits my websites and enters the zip code of delivery address and then my website shows the restaurants which deliver in that zip code. Some restaurants deliver in that zip code in less time and less shipping charges but some take more time and take more shipping charges.
What I have done so far is that I fetch the shipping codes from the database any restaurant delivers to. If the user’s zip code doesn’t exist in the list we alert else we move forward.
Next step is to fetch all the restaurants that deliver in that zip code in less delivery time and then restaurants that deliver in that zip code in more delivery time. Based on less or more delivery time I mark the user in Zone A or Zone B respectively.
I have flat rate shipping methods created for Zone A and Zone B which can be selected from the drop down in the Cart/Checkout Page. Now here it what the trouble is.
The user shouldn’t select what the shipping rate would be. It should be automatically selected and applied based on what Zone the user has been marked into.
This automatic updating is not what I am able to achieve.
What I have tried?
The right process should be to put this Zone into Woocommerce session and it should be picked automatically and applied when the user reaches the cart or checkout page.
I am not able to find the right method to call which could trigger the ajax call to update shipping. Any ideas would be helpful.
Regards
Use
$(body).trigger('update_checkout');
in your JS. This will fire off a request toWC_AJAX->update_order_review
, which will then pick up all the form data in the checkout area. If you need to pass any custom data, add a hidden input to the checkout and it should allow you to pass a value along through the$_POST
data.From there, if you need to mess with available methods and the session settings, you can do what I did and overwrite the
shipping_html
function in cart-shipping.php, like so.This is update proof, and it will let you customize that template by adding filters.
As for resetting the session to correspond with the selected method, I don’t have an answer for you, unfortunately, as I’m still wrestling with that exact problem myself.