【问题标题】:Assign custom field price as product price to specific user role in WooCommerce将自定义字段价格作为产品价格分配给 WooCommerce 中的特定用户角色
【发布时间】:2020-10-07 19:51:18
【问题描述】:

我创建了一个名为“批发商”的新角色。该角色按预期工作。

我还创建了一个名为“批发商价格”的自定义价格字段。它也可以按预期工作。

我检查用户角色,如果他们被分配为批发商,我给他们自定义产品价格(如果已填写)。我有一切工作,除了我无法弄清楚最后一块。如何提取当前产品 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 );
}

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

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce price


    【解决方案1】:

    你不需要你的函数check_user_role(),因为你可以以同样的方式使用 WordPress current_user_can()

    自 WooCommerce 3 以来,钩子 woocommerce_get_price 已被弃用。

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

    1) 在 WC_Product 对象上使用 get_meta() 方法(从 WooCommerce 3 开始)

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

    2) 或get_post_meta() 函数中的产品 ID(WordPress 旧方式)

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

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

    /* 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 您的答案不适用于可变产品
    • @WebDesign 这个答案只是回答了 OP 问题并为之工作......
    • 是的,它很有价值。谢谢
    猜你喜欢
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2020-11-19
    • 1970-01-01
    • 2019-04-24
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多