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:

 add_filter( 'woocommerce_add_to_cart_validation', 'addtocart_postcode_validation', 10, 5 );
 function addtocart_postcode_validation( $passed, $product_id, $quantity, $variation_id, $variations ) {

    // ==> Define HERE your array of targetted variations SLUGs values (NOT the Names)
    $variation_slugs = array('4-six-packs', '1-carton-(20-bottles)', '1-carton-(24 bottles)' );

    // ==> Define HERE your array of restricted states (coma separated)
    $restricted_states = array('South Australia');

    if ( is_user_logged_in() ){

        // Getting current user ID
        $user_id = get_current_user_id();

        // Getting current user address state
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // targeting users from South Australia
        if( in_array( $user_state, $restricted_states ) ){
            foreach( $variations as $variation ){
                // if the variation value is corresponding to a value of our arrays => true (OK)
                if( in_array( $variation, $variation_slugs ) ){
                    $passed = true; // OK
                } else {
                    $passed = false; // NOT OK
                    // Stops the loop
                    break;
                }
            }
        } else {
            $passed = true; // OK for other states
        }

        // (optionally) Displaying an alert message only for targetted state and if is not corresponding to targetted variations slugs
        if( !$passed )
            wc_add_notice( __( 'Sorry, you are not allowed to add this product with the chosen packaging, please chose another allowed packaging.', 'woocommerce' ), 'error' );


    } else { // for non logged user — NOT OK
        wc_add_notice( __( 'To add this product to cart, you need to be logged in.', 'woocommerce' ), 'error' );
        $passed = false;
    }

    return $passed;
}

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:

add_action('woocommerce_before_cart_table','output_cart_raw_data');
function output_cart_raw_data(){
    if ( !WC()->cart->is_empty() && is_user_logged_in() ){
        $user_id = get_current_user_id();
        $user_state = get_user_meta($user_id, 'billing_state', true);

        // Displaying the current user 'billing_state':
        echo '<br><div style="border:solid 2px #333; padding: 10px;"><p><b>User State:</b> <span style="font-style:italic; color: green;">'.$user_state.'</span></p>';

        // Iterating through each cart items
        $count = 1;
        foreach(WC()->cart->get_cart() as $cart_item){
            if($cart_item['variation_id'] > 0){
                echo "<p><b>Cart Item variable $count</b><br>";
                foreach( $cart_item['variation'] as $var_key => $var_value ){
                    echo '<span>Attribute — Key: <span style="font-style:italic; color: green;">'.$var_key.'</span> => Value: <span style="font-style:italic; color: green;">'.$var_value.'</span></span><br>';
                }
                echo '</p>'; $count++;
            }
        }
        echo '</div>';
    }
}

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