1
votes

In Woocommerce, I would like to set some requirements to be approved before customer to be able to checkout. I would like customer to have quantity of 6 before they can proceed to checkout.
Quantity is not related just to one product, they can combine more products until they reach total quantity.
If have exactly 6 , then can proceed. Next stage, must have exactly 12 quantity before can checkout (if have 8 quantity in cart for example), after that 18, 24 etc....

I am using this function, that works just for 6 items quantity count limitation. I would like too extend the functionality with the logic explained before, making it dynamic and progressive.

My actual code:

// Set a minimum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_num_products' );
function spyr_set_min_num_products() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set the minimum number of products before checking out
        $minimum_num_products = 6;
        // Get the Cart's total number of products
        $cart_num_products = WC()->cart->cart_contents_count;

        // Compare values and add an error is Cart's total number of products
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>' 
                . '<br />Current number of items in the cart: %s.',
                $minimum_num_products,
                $cart_num_products ),
            'error' );
        }
    }
}

Any help will be really appreciated.

1

1 Answers

1
votes

Here is the way to make a user dynamic progressive item quantity count limitation calculated when order status changes, with increase steps based on number six (6).

Also the quantity needs to be a multiple of six (6).

The code:

// User dynamic progressive item quantity count limitation
add_action( 'woocommerce_check_cart_items', 'user_dynamic_progressive_min_item_qty_count_limitation' );
function user_dynamic_progressive_min_item_qty_count_limitation() {
    // Only in the Cart or Checkout pages
    if( ! ( is_cart() || is_checkout() ) ) return; // Exit

    $base_step = 6; // The quantity step base
    $user_id   = get_current_user_id(); // User ID
    $min_qty   = get_user_meta( $user_id, '_min_item_qty_ref', true ); // Get min qty from user data
    $min_qty   = $user_id == 0 ? $base_step : ( empty($min_qty) ? $base_step : $min_qty );
    $qty_count = (int) WC()->cart->get_cart_contents_count(); // cart items count

    if( $qty_count < $min_qty ):

    // Display an error notice when quantity count is below the minimum
    wc_add_notice( sprintf( '<strong>A Minimum of %s products is required before checking out.</strong>'
        . '<br />Current number of items in the cart: %s.', $min_qty, $qty_count ), 'error' );

    elseif( ( $qty_count % 6 ) != 0 ):

    // Display an error notice when quantity count is not a multiple of 6
    wc_add_notice( sprintf( '<strong>A Multiple of %s products is required before checking out.</strong>'
        . '<br />Current number of items in the cart: %s.', $base_step, $qty_count ), 'error' );

    endif;
}

// User dynamic progressive item quantity count calculation (increase or decrease) on order status change
add_action('woocommerce_order_status_changed', 'user_dynamic_progressive_min_item_qty_calculation', 50, 4 );
function user_dynamic_progressive_min_item_qty_calculation( $order_id, $old_status, $new_status, $order ){

    $statuses_increase_limit = array('on-hold', 'processing', 'completed'); // Succesful statuses
    $statuses_decrease_limit = array('cancelled', 'failed'); // Negative statuses (decreasing)

    $min_qty_step = 6; // The quantity step base
    $customer_id  = (int) $order->get_customer_id(); // User ID
    $user_min_qty = (int) get_user_meta( $customer_id, '_min_item_qty_ref', true ); // Get min qty from user data
    $order_flag   = get_post_meta( $order_id, '_min_item_qty_flag', true ); // Get order min qty flag
    $order_flag   = empty($order_flag) ? false : $order_flag;

    if ( in_array($new_status, $statuses_increase_limit) && ! $order_flag )
    {
        $user_min_qty = $user_min_qty == 0 ? $min_qty_step : $user_min_qty;

        update_post_meta( $order_id, '_min_item_qty_flag', true );
        update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty + $min_qty_step  ); // Increase
    }
    elseif ( in_array($new_status, $statuses_increase_limit) && $order_flag )
    {
        update_post_meta( $order_id, '_min_item_qty_flag', false );
        update_user_meta( $customer_id, '_min_item_qty_ref', $user_min_qty - $min_qty_step ); // Decrease
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

When an order is placed, the calculation increase the min items quantity limitation count and is set in user data. The the order is flagged. If the order is cancelled or failed, it will check for the order flag and if the flag is set it will decrease the min items quantity limitation count.

enter image description here