将自定义字段价格作为产品价格分配给 WooCommerce 中的特定用户角色

use*_*824 5 php wordpress woocommerce price hook-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 );
}
Run Code Online (Sandbox Code Playgroud)

我可以给批发商一个固定百分比的报价,但这不是我的目标。

Loi*_*tec 6

您不需要您的功能check_user_role(),因为您可以以与 WordPress 相同的方式使用current_user_can()

此外,自 WooCommerce 3 起,该钩子woocommerce_get_price已被弃用。

要获得自定义批发商价格,您只需使用元键wholesaler_price即可,例如:

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

$price = (float) $product->get_meta( 'wholesaler_price' );
Run Code Online (Sandbox Code Playgroud)

get_post_meta()2) 或者在函数中使用产品 ID (WordPress 的旧方法)

$price = (float) get_post_meta( $product->get_id()  'wholesaler_price', true );
Run Code Online (Sandbox Code Playgroud)

现在在相关的挂钩代码函数中:

/* 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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。