WooCommerce Role-Based Pricing Setup with Meta Boxes

ACF ACF Guide Code Snippets Custom Fields PHP Tech Tutorials Woocommerce WooCommerce Customization WooCommerce Development Woocommerce Hooks WooCommerce Tips Wordpress WordPress Development

WooCommerce Role-Based Pricing Setup with Meta Boxes Tutorial/Guide

WooCommerce Role-Based Pricing Setup with Meta Boxes

  To personalize product pricing for wholesalers, resellers, or B2B buyers in WooCommerce, follow this detailed guide using custom user roles and meta boxes.

Step 1: Register Custom User Roles

Create new user roles using a plugin like “User Role Editor” or by placing this code inside your theme's functions.php file:

function create_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', 'create_custom_user_roles' );

Step 2: Assign the Roles to Users

From the WordPress user management screen, assign these roles accordingly for relevant customer types.

Step 3: Set Prices Per Role Using Product Meta

Add this function to apply custom prices to each role dynamically:

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

    if ( in_array( 'wholesaler', $user->roles ) ) {
        $price_data = get_post_meta( $product->get_id(), '_wholesaler_price', true );
        if ( $price_data ) return $price_data;
    } elseif ( in_array( 'reseller', $user->roles ) ) {
        $price_data = get_post_meta( $product->get_id(), '_reseller_price', true );
        if ( $price_data ) return $price_data;
    } elseif ( in_array( 'business_customer', $user->roles ) ) {
        $price_data = get_post_meta( $product->get_id(), '_business_customer_price', true );
        if ( $price_data ) return $price_data;
    }

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

Step 4: Create Meta Boxes for Price Input

Use this code to show meta fields in the product editor:

function register_price_meta_boxes() {
    add_meta_box( 'wholesaler_price', 'Wholesaler Price', 'show_wholesaler_price_box', 'product', 'normal' );
    add_meta_box( 'reseller_price', 'Reseller Price', 'show_reseller_price_box', 'product', 'normal' );
    add_meta_box( 'business_customer_price', 'Business Customer Price', 'show_business_price_box', 'product', 'normal' );
}
add_action( 'add_meta_boxes', 'register_price_meta_boxes' );

// Meta box display functions
function show_wholesaler_price_box( $post ) {
    $price = get_post_meta( $post->ID, '_wholesaler_price', true );
    echo '<label>Wholesaler Price:</label> <input type="number" step="0.01" name="wholesaler_price" value="' . esc_attr( $price ) . '" />';
}
function show_reseller_price_box( $post ) {
    $price = get_post_meta( $post->ID, '_reseller_price', true );
    echo '<label>Reseller Price:</label> <input type="number" step="0.01" name="reseller_price" value="' . esc_attr( $price ) . '" />';
}
function show_business_price_box( $post ) {
    $price = get_post_meta( $post->ID, '_business_customer_price', true );
    echo '<label>Business Customer Price:</label> <input type="number" step="0.01" name="business_customer_price" value="' . esc_attr( $price ) . '" />';
}

// Save meta values
function save_price_meta_boxes( $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_price_meta_boxes' );

This lets you manage pricing directly from the backend, with logic automatically applied on the front end.

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.