woocommerce check zip code before placing order

2020-05-24 10:37发布

问题:

As local delivery is the only option (due to product delivery restrictions) I do not want a customer to get to the checkout page and have to fill out all their details and only then discover we do not deliver to their postcode.

Therefore, I require the same functionality of the Local Delivery postcode check at the Checkout page, but to be added at an earlier stage in the checkout process, such as on the Cart page? Or any other page, for that matter. Best place can be in product page before add to cart option.

i.e. Enter your postcode to see if we deliver to your area: Result - a yes or no message appears with further instructions

回答1:

You can add a new field to the cart by using the woocommerce_cart_coupon hook and then you can create a handler using the template_redirect hook.

Something like the below which we have used on our sites before:

add_action( 'woocommerce_cart_coupon', array(&$this, 'new_woocommerce_cart_coupon'), 10, 0 );
add_action( 'template_redirect', array(&$this, 'new_post_code_cart_button_handler') );

public function new_woocommerce_cart_coupon() {
    ?>
        <br/><br/><p>Enter your postcode</p><label for="post_code">Post Code</label> <input type="text" name="post_code" class="input-text" id="post_code" value="" /> <input type="submit" class="button" name="apply_post_code" value="Check Post Code" />
    <?php
}

public function new_post_code_cart_button_handler() {
    if( is_cart() && isset( $_POST['post_code'] ) && $_SERVER['REQUEST_METHOD'] == "POST" && !empty( $_POST['post_code'] ) ) {
      //validate post code here
    }
}


回答2:

Sounds like your best option is to put the PostCode field at the top of your Billing Details form.

This way, once the PostCode is filled up, the shipping methods will adjust accordingly. As soon as the user goes down the form, the Local Shipping method will no longer be available if their postcode don't allow it.

This solution will only work if you place all the postcodes that you actually deliver to in the PostCode section in the Local Delivery settings in the WooCommerce dashboard:

This will ensure that Local Delivery option will only appear to the postcodes in your list. If the postcode entered is not on the list, the Local Delivery option will disappear on the shipping methods options below the form.

Just add this to your functions.php file:

//Rearrange the Fields in the Checkout Billing Details Form
add_filter("woocommerce_checkout_fields", "new_order_fields");

function new_order_fields($fields) {

$order_list = array(
    "billing_postcode",
    "billing_first_name", 
    "billing_last_name",
    "billing_email", 
    "billing_phone",        
    "billing_company", 
    "billing_address_1", 
    "billing_address_2",         
    "billing_country"        

);
foreach($order_list as $field)
{
    $ordered_fields[$field] = $fields["billing"][$field];
}

$fields["billing"] = $ordered_fields;
return $fields;

}

Just rearrange the $order_list array according to your preference.