Different Product Prices by Role in WooCommerce

Different Product Prices by Role in WooCommerce

 

Want to offer different product pricing to business types like wholesalers or resellers? This guide shows how to set that up in WooCommerce using meta boxes and user roles.

Step 1: Add Custom User Roles

Create dedicated user roles using the following snippet or a plugin:

function define_custom_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', 'define_custom_roles' );

Step 2: Assign Roles to Customers

In the admin panel, assign roles like `wholesaler` to registered users as needed.

Step 3: Override Product Prices by Role

Use this filter to return a specific price if the logged-in user has a matching role:

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

    $roles = $user->roles;

    foreach ( [ 'wholesaler', 'reseller', 'business_customer' ] as $role ) {
        if ( in_array( $role, $roles ) ) {
            $role_price = get_post_meta( $product->get_id(), '_' . $role . '_price', true );
            if ( $role_price ) return $role_price;
        }
    }

    return $price;
}
add_filter( 'woocommerce_product_get_price', 'override_price_by_user_role', 10, 2 );

Step 4: Add Custom Pricing Meta Boxes

Enable admin pricing input for different roles:

function register_role_price_boxes() {
    $roles = ['wholesaler', 'reseller', 'business_customer'];
    foreach ( $roles as $role ) {
        add_meta_box( "{$role}_price_box", ucfirst($role) . ' Price', function($post) use ($role) {
            $val = get_post_meta( $post->ID, "_{$role}_price", true );
            echo '<label>Enter Price for ' . ucfirst($role) . ':</label> ';
            echo '<input type="number" step="0.01" name="' . $role . '_price" value="' . esc_attr($val) . '" />';
        }, 'product', 'normal' );
    }
}
add_action( 'add_meta_boxes', 'register_role_price_boxes' );

function save_role_prices( $post_id ) {
    foreach ( ['wholesaler_price', 'reseller_price', 'business_customer_price'] as $field ) {
        if ( isset( $_POST[$field] ) ) {
            update_post_meta( $post_id, '_' . $field, sanitize_text_field( $_POST[$field] ) );
        }
    }
}
add_action( 'save_post_product', 'save_role_prices' );

Once complete, your store will show different prices to different users based on their role — fully automated.

Leave a Comment

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.