Categories

Monday, September 30, 2024
#919814419350 therichposts@gmail.com
DokanmultivendorWoocommerceWoocommerce HooksWordpress

How to restrict product sales by zip code in Dokan Multi Vendor?

How to restrict product sales by zip code in Dokan Multi Vendor?

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:

  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks
  4. Dokan


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:

  1. 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.
  1. 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.
  1. 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:

  1. Create a Custom Plugin:
    You can create a custom plugin or add this to your theme’s functions.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 );
  1. 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.
  2. 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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.