Copy this code in functions.php, and you will see a input box below ‘Add to cart‘ button.
But before this you have to add pin codes in your admin dashboard -> WooCommerce -> Settings -> Shipping -> Shipping Zones -> Zone Region (list available pin codes here)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | // Pincode check ---------> add_action( 'woocommerce_after_add_to_cart_quantity', 'woo_show_some_text', 20 ); function woo_show_some_text() { echo '<div class="pincodeSearch"> <label>Delivery</label> <input type="text" name="pincode" autocomplete="off" placeholder="Enter Delivery Pincode"> <div id="pincodeButton"> Check <span id="pinLoader">Wait...</span> </div> <div id="picodeRes"></div> </div>'; ?> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> <script> $(document).ready(function(){ $("#pincodeButton").click(function(){ $pincode = $('input[name="pincode"]').val(); if($pincode==""){ //$('#picodeRes').html('<span style="color:yellow;">Please type pincode</span>'); return false; } $('#pinLoader').show(); $('#picodeRes').html(''); $action = 'checkPincode'; jQuery.ajax({ type: "POST", url: "/wp-admin/admin-ajax.php", data: { action : $action, pincode : $pincode }, success: function($res){ $('#pinLoader').hide(); if($res=="1"){ $('#picodeRes').html('<span style="color:green;">Delivery available</span>'); }else{ $('#picodeRes').html('<span style="color:red;">Sorry! Delivery not available</span>'); } } }); }); }); </script> <?php } function checkPincode(){ if(!empty($_POST)){ if(is_numeric($_POST['pincode']) && $_POST['pincode']!=""){ $pincode = wp_check_invalid_utf8($_POST['pincode']); $delivery_zones = WC_Shipping_Zones::get_zones(); $i=1; foreach ( (array) $delivery_zones as $key => $the_zone ) { $zone_array = $delivery_zones[$i]['zone_locations']; $available_pincodes = array(); foreach($zone_array as $zone_arrayVal){ $available_pincodes[] = $zone_arrayVal->code; } if(in_array($pincode, $available_pincodes)){ echo "1"; }else{ echo "0"; } $i++; } die(); }else{ echo'-1'; die(); } } } add_action( 'wp_ajax_checkPincode', 'checkPincode' ); add_action( 'wp_ajax_nopriv_checkPincode', 'checkPincode' ); |