woocommerce check zip code before placing order

我怕爱的太早我们不能终老 提交于 2019-12-03 03:59:30

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
    }
}
lartan

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!