Skip to content
Advertisement

Requiring minimum variation quantity for customer from a specific state

In our Woocommerce shop, we have 2 variable products (a soft drink) in 330 ml and 500 ml.

The 330 ml product has variations:

  • 1 six pack
  • 2 six packs
  • 3 six packs
  • 4 six packs
  • 1 carton (24 bottles)

The 500 ml product has variations:

  • 1 six pack
  • 2 six packs
  • 3 six packs
  • 4 six packs
  • 1 carton (20 bottles)

We’re only selling to:

  • Western Australia
  • South Australia
  • Northern Territory

We have successfully restricted sales to the 3 states above, and we’re not selling to Victoria, Tasmania, New South Wales, or Queensland at the moment (we’re focusing on the 3 western most states).

How do we require customers in South Australia (SA) to purchase a minimum of:

  • 4 six packs of the 300 ml product
  • or a carton of the 300 ml product
  • or 4 six packs of the 500 ml product
  • or 1 carton of the 500 ml product

Advertisement

Answer

Here is a custom function hooked in woocommerce_add_to_cart_validation filter hook, that is going to make exactly what you are expecting.

But you will need to set correctly the data in the arrays in the beginning of the code inside the function as asked. If your conditions are not matched, the product will not be added to cart and (optionally) a custom message will be displayed.

Here is that code:

JavaScript

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Optionally you can display a custom message when wrong variations are submited and when the customer is not logged in (on product add to cart submission)…

This code is tested and works…


If you are unsure of the data that you have to set in the arrays, you can use before this little function, that will output on cart page:

  • the current user state,
  • the variation slugs of the items added in cart

Here is that code:

JavaScript

This code goes in function.php file of your active child theme (or theme)…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement