Woocommerce 购物车和批发角色的单个产品页面中的最小和步数

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

除了购物车页面上的非批发用户,我有这个工作。

要求:

这应该可行,但是,当我需要它时它会将购物车中的规则应用于所有用户以仅影响批发用户。

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;
    } 
}

更新:我测试了你的代码,有一些小问题和错误。

第一个函数 似乎有效,但存在一些错误,不建议对用户角色使用current_user_can() 条件函数。您不需要 else 中的所有属性键,因为大多数默认值已经是 1

第二个功能'input_value'和'step'没有任何效果,因为它们不存在于$args数组(参见下面的官方代码片段或该钩子的源代码)。缺少一些参数,例如 $product.

两个函数条件都有些错误。我已将您的条件 is_checkout() 替换为 is_product(),因为您在问题中提到的是针对单个产品页面和购物车页面。

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 ).

您可以在这个官方 WooCommerce 片段上查看代码:Adjust the quantity input values

此重新访问的代码现在也应该可以在购物车页面上运行:

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;
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已经过测试并且几乎可以正常工作。

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…