Last active
December 30, 2022 14:23
-
-
Save jorpdesigns/03f677d3ce4db826d8ad1baf5c0fd372 to your computer and use it in GitHub Desktop.
Snippet to remove billing fields on WooCommerce checkout page if the selected shipping method is Local Pickup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// FUNCTION TO CHECK IF SELECTED SHIPPING METHOD IS LOCAL PICKUP | |
function is_local_pickup() { | |
$local_pickup = false; | |
$shipping_method = 'local_pickup:9'; // Replace 9 with your local pickup id | |
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); | |
$chosen_shipping = $chosen_methods[0]; | |
if ($chosen_shipping == $shipping_method) { | |
$local_pickup = true; | |
} | |
return $local_pickup; | |
} | |
// DISABLE BILLING FIELDS | |
add_filter('woocommerce_checkout_fields', 'remove_billing_checkout_fields'); | |
function remove_billing_checkout_fields($fields) { | |
if ( is_local_pickup() ) { | |
unset($fields['billing']['billing_company']); | |
unset($fields['billing']['billing_country']); | |
unset($fields['billing']['billing_address_1']); | |
unset($fields['billing']['billing_address_2']); | |
unset($fields['billing']['billing_city']); | |
unset($fields['billing']['billing_state']); | |
unset($fields['billing']['billing_postcode']); | |
unset($fields['order']['order_comments']); | |
} | |
return $fields; | |
} | |
// REMOVE "Deliver to another address" PROMPT | |
add_action( 'template_redirect', function() { | |
if ( is_local_pickup() ) { | |
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false'); | |
} | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment