Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update shipping calculations on change of custom shipping field in woocommerce?

I have added new custom shipping field ( select option ) to woocommerce checkout page.

I am using that for shipping calculations.

that works perfect as well. but issue is while I change values in that field it do not update instantly.

It gives correct calculations on next page, after page submit. I need it to work as change in custom field.

How to trigger WooCommerce Ajax which updates shipping calculation on change of my custom field ?

like image 206
Jayesh Dhudashia Avatar asked Sep 06 '14 18:09

Jayesh Dhudashia


People also ask

How do I auto calculate shipping in WooCommerce?

Go to WooCommerce > Settings > ACS Web Services. Check Enable ACS Automatic Shipping Calculation and press Save. On Weight you will be able to select Automatic Weight if you want to calculate the shipping fee based on order's total weight or select flat to set a fixed order weight.

How do I add custom shipping charges in WooCommerce?

Head to: WooCommerce > Settings > Shipping. Select the Shipping Zone that Flat Rate should be added to and select Edit. In the Shipping Methods box, select Add Shipping Method. Select Flat Rate shipping from the dropdown and then Add Shipping method.


1 Answers

This is actually extremely simple to do if you are adding your fields in the correct way(using the woocommerce_checkout_fields filter). The only thing you need to do is to add the classes address-field and update_totals_on_change like this:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {

    $fields['shipping']['custom_field'] = array(
        'label' => 'Custom field',
        'required' => 1,
        'class' => array ('address-field', 'update_totals_on_change' )
    );

    return $fields;
} 
like image 97
Pelmered Avatar answered Oct 03 '22 07:10

Pelmered