簡體   English   中英

將自定義字段價格作為產品價格分配給 WooCommerce 中的特定用戶角色

[英]Assign custom field price as product price to specific user role in WooCommerce

我創建了一個名為“批發商”的新角色。 該角色按預期工作。

我還創建了一個名為“批發商價格”的自定義價格字段。 它也可以按預期工作。

我檢查用戶角色,如果他們被分配為批發商,我給他們定制產品價格(如果已填寫)。 除了我無法弄清楚最后一塊之外,我一切正常。 如何提取當前產品 ID,獲取批發價並分配。 我的functions.php代碼如下:

/* Custom user roles */
add_role( 'wholesaler', 'Wholesaler', get_role( 'customer' )->capabilities );

add_action( 'woocommerce_product_options_pricing', 'action_woocommerce_product_options_pricing', 10, 0 );

/* Add custom wholesaler price field to general page */
function action_woocommerce_product_options_pricing() { 
    woocommerce_wp_text_input( array(
        'id' => 'wholesaler_price', 
        'class' => 'wc_input_price short', 
        'label' => __( 'Wholesaler price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
    ) );
}

// Save Fields
function action_woocommerce_admin_process_product_object( $product ) {
    if( isset($_POST['wholesaler_price']) ) {
        $product->update_meta_data( 'wholesaler_price', sanitize_text_field( $_POST[ 'wholesaler_price'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

/* Custom prices by user role */
add_filter('woocommerce_get_price', 'custom_price_assign', 10, 2);

function custom_price_assign($price, $product) {
    if (!is_user_logged_in()) return $price;

        // Check if the user has a role of wholesaler
        if (check_user_role('wholesaler')){
            $price = $price; // Assign wholesale price for product (if it is set for that product)
        }

    return $price;
}

/* Check user role */
function check_user_role($role = '',$user_id = null){
    if ( is_numeric( $user_id ) )
        $user = get_user_by( 'id',$user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
        return false;

    return in_array( $role, (array) $user->roles );
}

我可以給批發商一個固定百分比的報價,但這不是我的目標。

您不需要 function check_user_role() ,因為您可以以相同的方式使用 WordPress current_user_can()

自 WooCommerce 3 起,鈎子woocommerce_get_price也已被棄用。

要獲得您的自定義批發商價格,您只需使用元密鑰wholesaler_price價格,例如:

1) 使用WC_Product Object 上的get_meta()方法(自 WooCommerce 3 起)

$price = (float) $product->get_meta( 'wholesaler_price' );

2) 或者使用get_post_meta() function 中的產品 ID (WordPress 舊方式)

$price = (float) get_post_meta( $product->get_id()  'wholesaler_price', true );

現在在您的相關掛鈎代碼 function 中:

/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)

function custom_price_assign( $price, $product ) {
    // Check if the user has a role of wholesaler
    if ( current_user_can('wholesaler') && $wholesaler_price = $product->get_meta('wholesaler_price') ){
        return $wholesaler_price;
    }
    return $price;
}

代碼進入您的活動子主題(或活動主題)的functions.php 文件。 測試和工作。

暫無
暫無

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

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