为了账号安全,请及时绑定邮箱和手机立即绑定

根据WooCommerce中的用户角色更改数量步骤

根据WooCommerce中的用户角色更改数量步骤

PHP
杨__羊羊 2021-05-06 18:17:24
在WooCommerce中,我尝试根据用户角色和产品来逐步更改“产品数量”字段设置。这是我的代码add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 ); // Simple productsfunction 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;}如何使此代码也适用于用户角色?
查看完整描述

1 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您的代码$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文件中。它应该工作。


查看完整回答
反对 回复 2021-05-14
  • 1 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号