Home Min and Steps quantity in Woocommerce cart and single product page for wholesale role
Reply: 1

Min and Steps quantity in Woocommerce cart and single product page for wholesale role

user2059376
1#
user2059376 Published in 2018-01-13 00:20:30Z

I have this working except for non-wholesale users on the cart page.

Requirements:

  • Min quantity of 36
  • intervals of 36
  • Apply to single-product-page & cart
  • current_user_can('wholesale')
  • has_term( 'mug', 'product_tag')

This should work but, it applies the rules in the cart to all users when I need it to effect only wholesale users.

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
    if ( is_cart() || is_checkout()  && has_term( 'mug', 'product_tag') && 
    current_user_can('wholesale') ) {
        $args['input_value'] = 36; // Start from this value (default = 1) 
        $args['min_value']   = 36; // Minimum value
        $args['step']        = 36; // Quantity steps
        return $args;
    } else {
        $args['input_value'] = 1; // Start from this value (default = 1) 
        $args['min_value']   = 1; // Minimum value
        $args['step']        = 1; // Quantity steps
        return $args;
    }
}

// Variations
add_filter( 'woocommerce_available_variation', 'jk_woocommerce_available_variation' );
function jk_woocommerce_available_variation( $args ) {
    if (  has_term( 'mug', 'product_tag') && 
    current_user_can('wholesale') ) {
        $args['input_value'] = 36; // Start from this value (default = 1) 
        $args['min_value']   = 36; // Min quantity (default = 1)
        $args['step']        = 36; // Increment/decrement by this value (default = 1)
        return $args;
    } else {
        $args['min_value']   = 1; // Minimum value
        $args['step']        = 1; // Quantity steps
        $args['input_value'] = 1; // Starting value (we only want to affect product pages, not cart)
        return $args;
    } 
}
LoicTheAztec
2#
LoicTheAztec Reply to 2018-01-13 05:56:09Z

Updated: I have tested your code and there is some gliches and mistakes.

The first function seems to work but there is some errors and is not recommended to use current_user_can() conditional function for user roles. You don't need all attributes keys in the else, as most of defaults values are already 1

The second function keys 'input_value' and 'step' doesn't have any effect as they doesn't exist in the $args array (see the official snippet code below or the source code for this hook). There is some missing arguments like the $product.

In both functions there is some mistakes in the condition. I have replaced in your condition is_checkout() by is_product() as you are targeting single product pages and cart page as you mention in your question.

You should check and be sure that the correct Wholesale user role slug is 'wholesale' (as it could be for example 'wholesale_customer' like in this question).

You can check code on this official WooCommerce snippet: Adjust the quantity input values…

This revisted code should work on cart page too now:

add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    $user = wp_get_current_user(); // Get current WP_User
    if ( ! ( is_cart() || is_product() ) ) return $args;
    if ( ! in_array( 'wholesale', $user->roles ) ) return $args;
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    if ( ! has_term( 'mug', 'product_tag', $product_id ) ) return $args;

    // $args['input_value'] = 36; // Start from this value (default = 1)
    $args['min_value']   = 36; // Min value (default = 0)
    $args['step']        = 36; // Quantity steps (default = 1)

    return $args;
}

add_filter( 'woocommerce_available_variation', 'custom_qty_available_variation_args', 10, 3 );
function custom_qty_available_variation_args( $data, $product, $variation ) {
    $user = wp_get_current_user(); // Get current WP_User
    if ( ! ( is_cart() || is_product() ) ) return $data;
    if ( ! in_array( 'wholesale', $user->roles ) ) return $data;
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if ( ! has_term( 'mug', 'product_tag', $product_id ) ) return $data;

    $data['min_qty'] = 36; // Minimum value (default = 1)
    $args['input_value'] = 36; // Start from this value (default = 1)
    $args['min_value']   = 36; // Min value (default = 0)
    $args['step']        = 36; // Quantity steps (default = 1)

    return $data;
}

Code goes in function.php file of the active child theme (or active theme).

Tested and and almost working.

It seems that there is a bug in cart page. The products get stucked on 36, even if you increase and update quantities for both simple and variations products…

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.239946 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO