Woocommerce updates delivery methods at checkout through ajax

What I'm trying to accomplish: In woocommerce, I need to check the date sent using the datepicker field when the user selects the date and then updates the delivery parameters accordingly via ajax so that things like free shipping can be exported when they do not fit.

What I currently know / understood: I am currently triggering a jQuery event and dispatch it through the user script date I need. I could not find a function in the woocommerce classes that returns only delivery data, so I don’t think I can name it and return it as a fragment, as is done for verification.

However, I found that

WC_AJAX::update_order_review() 

has an action call inside it, and I successfully connected the function to this action, and I was able to disable

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

which disables the action that updates the validation check block.

My real question is: All this is great, and I look like its title is in the right direction, but I don’t know woocommerce well enough to know how to get the delivery methods inside my connected action to cancel them as necessary. Does anyone know if I can get them through the global $ woocommerce object and then ask them to read the rest of this WC_AJAX method?

Any help here would be greatly appreciated.

** Notes: yes, I know that it is "t", not "$" in jQuery. No mistake

+7
ajax php wordpress datepicker woocommerce
source share
3 answers

So, for those of you who might need it, they need a little chip in woocommerce because of how they handle the output of basket / statement values ​​for shipping and payment options, but in the end I could do it using woocommerce hooks and very a small modification to the plugin, so a pretty clean solution.

Basically, I used woocommerce sessions, two hooks and a javascript trigger that updates check order validation.

Thanks to the combination of the woocommerce_checkout_update_order_review hook that occurs in WC_AJAX::update_order_review and the use of the called call $( 'body' ).trigger( 'update_checkout' ); , I can connect to an action that updates the validation check block. So far so good.

What I did in this function was to check if the field mattered, and if that happened, save it in WC()->session . THEN, since WC_AJAX::update_order_review calls woocommerce_order_review() to grab all the updated delivery and delivery methods, I looked at the template associated with this function and found another hook woocommerce_review_order_before_shipping that allows me to modify the delivery methods until the basket template moves and builds delivery options in wc_cart_totals_shipping_html() .

Now, I know, you think, wait, I need delivery methods, not carts, but in reality they seem to be the same.

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

Greetings.

+6
source share

So Trevor’s answer didn’t quite work for me (maybe the WC has changed since it was made), it really led me in the right direction.

I used several several different filters. The problem I need to solve is to add / remove specific delivery options based on a custom field.

 add_action('woocommerce_checkout_update_order_review', function ($rawFields) { // This function checks for the value of my custom field on the checkout page. parse_str($rawFields, $checkoutFields); $this->myField = $checkoutFields['my_field'] ?? null; }); add_filter('woocommerce_shipping_packages', 'modifyShippingPackages']); public function modifyShippingPackages($packages) { if ($this->myField) { //.. modify $packages. In my case, I had to remove shipping methods. $rates = $packages[0]['rates']; $newRates = array_filter($rates, function ($shippingRate) { // figure out what should be in the rates }); $packages[0]['rates'] = $newRates; $session = WC()->session; $session->set('shipping_method_counts', [count($newRates)]); $session->set('chosen_shipping_methods', [$newRates[0]->id]); } return $packages; } 

The above snippet is an incomplete version of my code, but should get a point. It can be easily tested by typing jQuery( 'body' ).trigger( 'update_checkout' ); in your javascript console.

Thanks for the Trevor tips.

+3
source share

If someone wants to update the cart:

 foreach (WC()->cart->get_cart() as $key => $value) { WC()->cart->set_quantity($key, $value['quantity']+1); WC()->cart->set_quantity($key, $value['quantity']); break; } 

Change the quantile, then change it. And the delivery is recounted.

(more than 5 hours of viewing the code, next time I will code the entire online store, just use a good structure and a full custom OMG)

+1
source share

All Articles