简体   繁体   English

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

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

I have this working except for non-wholesale users on the cart page. 除购物车页面上的非批发用户外,我可以进行此工作。

Requirements: 要求:

  • Min quantity of 36 最少数量36
  • intervals of 36 间隔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;
    } 
}

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. 第一个函数似乎起作用,但是存在一些错误,建议不要对用户角色使用current_user_can()条件函数。 You don't need all attributes keys in the else, as most of defaults values are already 1 您不需要else中的所有属性键,因为大多数默认值已经是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) . 第二个功能'input_value'和'step'无效,因为它们在$args数组中不存在(请参见下面的官方代码段或此钩子的源代码) There is some missing arguments like the $product . 有一些缺少的论点,例如$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. 我已将条件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 this question ) . 您应该检查并确保正确的批发用户角色信息是'wholesale' (例如,像这个问题一样,可能是'wholesale_customer'

You can check code on this official WooCommerce snippet: Adjust the quantity input values 您可以在此WooCommerce官方代码段上检查代码: 调整数量输入值

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). 代码进入活动子主题(或活动主题)的function.php文件中。

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… 即使您增加和更新简单和变动产品的数量,产品也会卡在36上…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce 更改购物车中每个产品的数量最小限制 - Woocommerce change quantity min limit for each product in cart 在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计 - Display product total and cart subtotal on quantity change in WooCommerce product page 在woocommerce单个产品页面中添加固定数量的添加到购物车按钮 - additional add to cart button with fixed quantity in woocommerce single product pages WooCommerce $ product-> get_stock_quantity(); 打破购物车页面 - WooCommerce $product->get_stock_quantity(); breaking cart page 如何从woocommerce的购物车页面获取特定产品数量 - how to get particular product quantity from the cart page in the woocommerce woocommerce 添加到单个产品页面上的购物车按钮 - woocommerce add to cart button on single product page 根据WooCommerce中的用户角色更改数量步骤 - Change quantity steps depending on the user role of the in WooCommerce WooCommerce单品页面自动加入购物车 - Automatically add product to cart when visiting single product page in WooCommerce 计算 WooCommerce 中单个产品页面上数量增量的小计 - Calculate subtotal on quantity increment on single product page in WooCommerce woocommerce购物车数量未正确显示在购物车页面上 - woocommerce cart quantity not showing correctly on cart page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM