将自定义字段价格作为产品价格分配给WooCommerce中的特定用户角色 [英] Assign custom field price as product price to specific user role in WooCommerce

查看:47
本文介绍了将自定义字段价格作为产品价格分配给WooCommerce中的特定用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为批发商"的新角色.该角色将按预期工作.

I created a new role called "Wholesaler". The role works as expected.

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

I also created a custom price field called "Wholesaler price". It also works as expected.

我检查用户角色,如果他们被分配为批发商,则给他们自定义产品价格(如果已填写).除了我无法弄清楚最后一块,我一切都在工作.如何提取当前的产品ID,获取批发价并进行分配.我的functions.php代码如下:

I check the user role, if they are assigned as a wholesaler, I give them the custom product price (if it is filled in). I have everything working except I can't figure out the final piece. How do I pull the current product ID, get the wholesale price, and assign it. My functions.php code is below:

/* 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 );
}

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

I can give the wholesaler a fixed percentage offer but that isn't my goal.

推荐答案

您不需要功能 check_user_role(),就可以使用与WordPress

You don't need your function check_user_role() as you can use in a same way WordPress current_user_can().

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

Also the hook woocommerce_get_price is deprecated since WooCommerce 3.

要获取自定义批发商的价格,您只需使用元代码批发商价格,例如:

To get your custom wholesaler price you will simply use the meta key wholesaler_price like:

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

1) With get_meta() method on the WC_Product Object (since WooCommerce 3):

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

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

2) Or with the product ID in get_post_meta() function (the WordPress old way):

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

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

Now in your related hooked code 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文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于将自定义字段价格作为产品价格分配给WooCommerce中的特定用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆