Hello guys how are you? welcome back to my blog therichpost.com. Guys today in this blog post, I am going to tell you, How to restrict product sales by zip code in Dokan Multi Vendor?
Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:
Guys Here’s how you can proceed with both approaches:
1. Using WooCommerce Shipping Zones by Zip Code
Guys this method uses WooCommerce’s built-in shipping zone functionality to restrict sales by zip code.
Steps:
- Create Shipping Zones:
- In your WordPress admin, go to WooCommerce > Settings > Shipping > Shipping Zones.
- Click Add shipping zone.
- Define a Zone Name and set the Region.
- Under Postcodes, input the zip codes where you want the product to be available.
- Set Shipping Methods for the Zone:
- After creating the zone, assign shipping methods (e.g., Flat Rate, Free Shipping, etc.) that apply to those areas.
- Click Save changes.
- Restrict Product to Shipping Zones:
- You can now ensure the product is only purchasable for customers in those specific shipping zones.
- In the product settings, go to Shipping and ensure that the product is only available for shipping in the zones you’ve set up.
This way, if a customer tries to purchase the product from outside the specified zip codes, they won’t be able to complete the checkout.
2. Custom Plugin Approach for Zip Code Restriction
Guys if you want more control, like completely blocking the product from being viewed or added to the cart outside certain zip codes, here’s a basic outline for a custom solution:
Steps:
- Create a Custom Plugin:
You can create a custom plugin or add this to your theme’sfunctions.php
. Here’s a basic example:
function restrict_product_by_zip_code( $valid, $product_id ) { if ( ! is_checkout() ) { return $valid; } $allowed_zip_codes = array( '12345', '67890' ); // Define allowed zip codes $customer_zip_code = WC()->customer->get_shipping_postcode(); // Get customer zip code if ( ! in_array( $customer_zip_code, $allowed_zip_codes ) ) { wc_add_notice( __( 'Sorry, this product is not available for your zip code.', 'woocommerce' ), 'error' ); return false; } return $valid; } add_filter( 'woocommerce_add_to_cart_validation', 'restrict_product_by_zip_code', 10, 2 );
- Modify Product Page:
This will restrict the product during checkout, but you can also extend this to block the product from being visible in other areas based on the zip code. For example, you can hook into WooCommerce’s template to prevent the product from being added to the cart at all. - Testing:
After adding the custom code, test the functionality by trying to purchase the product using different zip codes to ensure only the defined ones are accepted.
Which approach would you like to proceed with, or would you like assistance implementing either method?
Guys if you will have any kind of query then feel free to comment below.
Thanks
Ajay
Recent Comments