Setting Different Prices by User Role in WooCommerce

Setting Different Prices by User Role in WooCommerce

To tailor your WooCommerce product prices for wholesalers, resellers, and business customers, follow this guide to set up different prices based on user roles.

Step 1: Define User Roles

First, create custom user roles for wholesalers, resellers, and business customers. You can use a plugin like “User Role Editor” or add the code below to your theme’s functions.php file:

function add_custom_user_roles() {
    add_role( 'wholesaler', 'Wholesaler', array( 'read' => true ) );
    add_role( 'reseller', 'Reseller', array( 'read' => true ) );
    add_role( 'business_customer', 'Business Customer', array( 'read' => true ) );
}
add_action( 'init', 'add_custom_user_roles' );
</pre

Step 2: Assign Roles to Users

In your WordPress dashboard, assign these roles to the respective users (wholesalers, resellers, and business customers).

Step 3: Set Custom Prices by Role

Now, define different prices based on user roles using the woocommerce_product_get_price filter. Add the following code to your theme’s functions.php or in a custom plugin:

function custom_product_price_based_on_user_role( $price, $product ) {
    $user = wp_get_current_user();

    if ( in_array( 'wholesaler', $user->roles ) ) {
        // Set wholesaler price
        $wholesaler_price = get_post_meta( $product->get_id(), '_wholesaler_price', true );
        if ( $wholesaler_price ) {
            return $wholesaler_price;
        }
    } elseif ( in_array( 'reseller', $user->roles ) ) {
        // Set reseller price
        $reseller_price = get_post_meta( $product->get_id(), '_reseller_price', true );
        if ( $reseller_price ) {
            return $reseller_price;
        }
    } elseif ( in_array( 'business_customer', $user->roles ) ) {
        // Set business customer price
        $business_customer_price = get_post_meta( $product->get_id(), '_business_customer_price', true );
        if ( $business_customer_price ) {
            return $business_customer_price;
        }
    }

    return $price; // Return original price if no custom price is set for the user role
}
add_filter( 'woocommerce_product_get_price', 'custom_product_price_based_on_user_role', 10, 2 );

Step 4: Add Meta Boxes for Custom Prices

Next, create meta boxes in the product editing screen to input custom prices for each role.

save_post_product action hook to save the corresponding meta box data.

function add_custom_price_meta_boxes() {
    add_meta_box( 'wholesaler_price_meta_box', 'Wholesaler Price', 'display_wholesaler_price_meta_box', 'product', 'normal', 'default' );
    add_meta_box( 'reseller_price_meta_box', 'Reseller Price', 'display_reseller_price_meta_box', 'product', 'normal', 'default' );
    add_meta_box( 'business_customer_price_meta_box', 'Business Customer Price', 'display_business_customer_price_meta_box', 'product', 'normal', 'default' );
}
add_action( 'add_meta_boxes', 'add_custom_price_meta_boxes' );

// Display meta boxes
function display_wholesaler_price_meta_box( $post ) {
    $wholesaler_price = get_post_meta( $post->ID, '_wholesaler_price', true );
    echo '';
    echo '';
}

function display_reseller_price_meta_box( $post ) {
    $reseller_price = get_post_meta( $post->ID, '_reseller_price', true );
    echo '';
    echo '';
}

function display_business_customer_price_meta_box( $post ) {
    $business_customer_price = get_post_meta( $post->ID, '_business_customer_price', true );
    echo '';
    echo '';
}

// Save meta box data
function save_custom_price_meta_box_data( $post_id ) {
    if ( isset( $_POST['wholesaler_price'] ) ) {
        $wholesaler_price = sanitize_text_field( $_POST['wholesaler_price'] );
        update_post_meta( $post_id, '_wholesaler_price', $wholesaler_price );
    }

    if ( isset( $_POST['reseller_price'] ) ) {
        $reseller_price = sanitize_text_field( $_POST['reseller_price'] );
        update_post_meta( $post_id, '_reseller_price', $reseller_price );
    }

    if ( isset( $_POST['business_customer_price'] ) ) {
        $business_customer_price = sanitize_text_field( $_POST['business_customer_price'] );
        update_post_meta( $post_id, '_business_customer_price', $business_customer_price );
    }
}
add_action( 'save_post_product', 'save_custom_price_meta_box_data' );

With these steps, WooCommerce will automatically display prices according to each user’s role, offering a personalized shopping experience for wholesalers, resellers, and business customers.

 

Scroll to Top

Request A Quote