【问题标题】:Change quantity steps depending on the user role of the in WooCommerce根据 WooCommerce 中的用户角色更改数量步骤
【发布时间】:2019-04-23 20:22:45
【问题描述】:

在 WooCommerce 中,我尝试根据用户角色和产品将产品数量字段设置更改为步骤。

这是我的代码

add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple products
function jk_woocommerce_quantity_input_args( $args, $product, $roles ) {
    if ( is_singular( 'product' ) ) {
        if( $product->get_id() == 85 ) {
            $args['input_value'] = 30;
        } else {
            $args['input_value'] = 5;
        }
    }

    if( array ( $roles, 'customer_roles')) {
        $args['min_value']      = 1;
         $args['step']           = 1;               
    }

    if( $product->get_id() == 85 ) {
            $args['min_value'] = 30;
            $args['step']      = 5;
        } else {
            $args['min_value'] = 5;
            $args['step']      = 5;
        }
        return $args;
}

如何使此代码也适用于用户角色?

【问题讨论】:

    标签: php woocommerce product hook-woocommerce product-quantity


    【解决方案1】:

    您的代码中有一些错误,例如 $roles 变量不存在此钩子参数和其他一些...尝试以下操作:

    add_filter( 'woocommerce_quantity_input_args', 'quantity_input_args_filter_callback', 10, 2 );
    function quantity_input_args_filter_callback( $args, $product ) {
        // HERE define the user role:
        $user_role = 'customer_roles';
    
        $user = wp_get_current_user(); // Get the WP_Use Object
    
        // For a specific user role, we keep default woocommerce quantity settings
        if( in_array( $user_role, $user->roles ) ) 
            return $args; // Exit to default
    
        // For the others as following
    
        if ( is_singular( 'product' ) ) {
            if( $product->get_id() == 85 ) {
                $args['input_value'] = 30;
            } else {
                $args['input_value'] = 5;
            }
        }
    
        if( $product->get_id() == 85 ) {
            $args['min_value'] = 30;
            $args['step']      = 5;
        } else {
            $args['min_value'] = 5;
            $args['step']      = 5;
        }
        return $args;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 2020-11-14
      • 2021-01-30
      • 2019-01-12
      • 2017-08-14
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多