Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce update shipping methods in checkout via ajax

What i'm trying to accomplish: In woocommerce, I need to check a date delivered via a datepicker field when user selects a date, and then update shipping options accordingly via ajax, so that things like free shipping can be taken out when they are not appropriate.

What I currently know/have figured out: I currently have the jQuery event firing and sending through a date to a custom script, which is kinda where I need to do. I have not been able to find a function within woocommerce classes that returns just the shipping data, so I don't think I can call that and return it as a fragment, as they do for the checkout already.

However, I did find that

WC_AJAX::update_order_review()

has an action call within it, and I have successfully hooked a function onto that action, AND i've been able to fire off the

t( 'body' ).trigger( 'update_checkout' );

which fires off the action that updates the checkout review block.

My real question: All this is great, and I'ts looking like its heading in the right direction, but I don't know woocommerce well enough to know how to get the shipping methods within my hooked action to unset them as necessary. Does anyone know if I can get them through the $woocommerce global object, and then have them be read by the rest of that WC_AJAX method??

Any help here would be greatly appreciated.

** notes: yes, I know thats a 't' and not a '$' in the jQuery. Not a mistake

like image 211
Trevor Miles Wagner Avatar asked Apr 21 '15 16:04

Trevor Miles Wagner


1 Answers

So, for those of you who may need this, it required a little finagling in woocommerce, because of the way they handle output of cart/checkout values for shipping and payment options, but in the end, I could make it happen using woocommerce's hooks, and very little plugin modification, so, a pretty clean solution.

Basically what I did was use woocommerce sessions, two hooks, and a javascript trigger that refreshes the checkout order review.

Through a combination of the woocommerce_checkout_update_order_review hook that happens in the WC_AJAX::update_order_review and use of the aformentioned $( 'body' ).trigger( 'update_checkout' ); call, I could hook in to the action that updates the checkout review block. So far so good.

What I did in this function was check if the field had a value, and if it did, save it in the WC()->session. THEN, since WC_AJAX::update_order_review calls woocommerce_order_review() to grab all the updated cart and shipping methods, I looked into the template associated with that function and found another hook, woocommerce_review_order_before_shipping, which allows me to modify the shipping methods before the cart template loops through and builds the shipping options in wc_cart_totals_shipping_html().

Now, I know, you're thinking, wait, I need checkout shipping methods, not cart ones, but in reality, they seem to be one and the same.

Hopefully my wasted hours will help someone else with a similar problem.

Cheers.

like image 154
Trevor Miles Wagner Avatar answered Oct 15 '22 15:10

Trevor Miles Wagner