簡體   English   中英

根據WooCommerce中的用戶角色更改數量步驟

[英]Change quantity steps depending on the user role of the in WooCommerce

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

如何使此代碼也適用於用戶角色?

您的代碼中存在一些錯誤,例如該鈎子參數不存在$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文件中。 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM