Home » WooCommerce: Ship to Predefined “Local Pickup” Addresses

WooCommerce: Ship to Predefined “Local Pickup” Addresses

by Tutor Aspire

WooCommerce Shipping Zones are quite easy to override / customize. With a simple PHP (and jQuery, sometimes) snippet we can accomplish many advanced shipping rules, such as the one we’ll study today.

This time, I’ve tested a snippet to add a dropdown to the billing section where users go choose the pickup destination. As a result, the shipping address is automatically populated, and so is the shipping method. What do you think?

1. Shipping Zones Setup

The very first thing you need to do is setting up the WooCommerce shipping zones. In this case, each zone will correspond to a Local Pickup Address (e.g. shop #1, shop #2, etc).

In the following example, I’ve added 2 zones in Australia, with the same state (Australian Capital Territory) but 2 different postcodes.

Each zone has its unique shipping method (Local Pickup) with cost = $0:

Advanced Local Pickup – WooCommerce Shipping Zones

2. Advanced Local Pickup – PHP/jQuery Snippet

Now that the zones are correctly setup, here comes the hard part. The following is a snippet that requires a lot of improvements, so if you spot anything or wish to contribute leave a comment below.

/**
 * @snippet       Advanced Local Pickup - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

//////////////////////////////////////////////////////////
// 1. Select field @ checkout billing section

add_filter( 'woocommerce_checkout_fields', 'tutoraspire_display_pickup_locations' );

function tutoraspire_display_pickup_locations( $fields ) {
$fields['billing']['pick_up_locations'] = array(
'type' => 'select',
'options' => array(
'option_1' => 'Select...',
'option_2' => 'Melbourne Road Shop',
'option_3' => 'Perth Road Shop'),
'label' => 'Pick Up Location',
'class' => array( 'form-row-wide' ),
'clear' => true,
);
return $fields;
}

//////////////////////////////////////////////////////////
// 2. Show field only when country == Australia @ checkout

add_action( 'woocommerce_after_checkout_form', 'tutoraspire_conditionally_hide_show_pickup', 5 );

function tutoraspire_conditionally_hide_show_pickup() {
wc_enqueue_js( "
$('select#billing_country').change(function(){
var country = $(this).val();
if ( country == 'AU' ) {
$('#pick_up_locations_field').fadeIn();
} else {
$('#pick_up_locations_field').fadeOut();
$('#pick_up_locations_field input').val('');
}
});
" );
}

//////////////////////////////////////////////////////////
// 3. "Ship to a different address" opened by default

add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );

//////////////////////////////////////////////////////////
// 4. Change shipping address when local pickup location changes

add_action( 'woocommerce_after_checkout_form', 'tutoraspire_checkout_update_pickup_address', 10 );

function tutoraspire_checkout_update_pickup_address() {
wc_enqueue_js( "
$('select#pick_up_locations').change(function(){
var location = $(this).val();
if (location == 'option_2') {
$('select#shipping_country').val('AU').change();
$('select#shipping_state').val('ACT').change();
$('#shipping_city_field input').val('Sidney');
$('#shipping_address_1_field input').val('Melbourne Road');
$('#shipping_postcode_field input').val('34500');
$('.shipping_address input[id^='shipping_']').prop('disabled', true);
$('.shipping_address select[id^='shipping_']').prop('disabled', true);
} else if (location == 'option_3') {
$('select#shipping_country').val('AU').change();
$('select#shipping_state').val('ACT').change();
$('#shipping_city_field input').val('Sidney');
$('#shipping_address_1_field input').val('Perth Road');
$('#shipping_postcode_field input').val('79200');
$('.shipping_address input[id^='shipping_']').prop('disabled', true);
$('.shipping_address select[id^='shipping_']').prop('disabled', true);
} else {
$('.shipping_address input[id^='shipping_']').prop('disabled', false);
$('.shipping_address select[id^='shipping_']').prop('disabled', false);
}
});
" );
}

As a result, once you select the correct local pickup address form the billing section, you should automatically get the correct shipping method in the checkout:

Advanced Local Pickup – WooCommerce Checkout

Did you test this? Do you have anything useful to add? Let me know!

You may also like