Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

woocommerce check zip code before placing order

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

like image 491
Ashfaq Rahman Avatar asked Feb 25 '26 10:02

Ashfaq Rahman


1 Answers

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
    }
}
like image 63
William Oetting Avatar answered Feb 26 '26 23:02

William Oetting