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

心已入冬 提交于 2020-12-12 18:17:50

问题


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.

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.


回答1:


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

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) With get_meta() method on the WC_Product Object (since WooCommerce 3):

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

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

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



来源:https://stackoverflow.com/questions/62438406/assign-custom-field-price-as-product-price-to-specific-user-role-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!