Different Prices According To User Role Woocommerce

To set different prices for wholesalers, resellers, and business customers in WooCommerce, you can follow these steps:

 

Step 1: Create User Roles You’ll first need to create user roles for wholesalers, resellers, and business customers. You can do this using a plugin like “Members” or “User Role Editor.” Alternatively, you can add the following code to your theme’s functions.php file to create the user roles:

 

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' );

 

Step 2: Assign User Roles Assign the appropriate user roles to your wholesalers, resellers, and business customers via the WordPress admin panel.

Step 3: Set Up Different Prices for Each User Role Now, you can set different prices for each user role. To do this, you can use the woocommerce_product_get_price filter hook. Add the following code to your theme’s functions.php file or 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; // If no custom price is set for the user role, return the original price
}
add_filter( 'woocommerce_product_get_price', 'custom_product_price_based_on_user_role', 10, 2 );

 

Step 4: Add Meta Boxes for Custom Prices Now, you need to add meta boxes for setting custom prices for each user role. You can use the add_meta_boxes action hook to create the meta boxes and the save_post_product action hook to save the 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' );

function display_wholesaler_price_meta_box( $post ) {
    $wholesaler_price = get_post_meta( $post->ID, '_wholesaler_price', true );
    echo '<label for="wholesaler_price">Enter Wholesaler Price:</label>';
    echo '<input type="number" step="0.01" name="wholesaler_price" id="wholesaler_price" value="' . esc_attr( $wholesaler_price ) . '">';
}

function display_reseller_price_meta_box( $post ) {
    $reseller_price = get_post_meta( $post->ID, '_reseller_price', true );
    echo '<label for="reseller_price">Enter Reseller Price:</label>';
    echo '<input type="number" step="0.01" name="reseller_price" id="reseller_price" value="' . esc_attr( $reseller_price ) . '">';
}

function display_business_customer_price_meta_box( $post ) {
    $business_customer_price = get_post_meta( $post->ID, '_business_customer_price', true );
    echo '<label for="business_customer_price">Enter Business Customer Price:</label>';
    echo '<input type="number" step="0.01" name="business_customer_price" id="business_customer_price" value="' . esc_attr( $business_customer_price ) . '">';
}

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, you should now have different prices set for wholesalers, resellers, and business customers in WooCommerce using meta boxes. The prices will be displayed based on the user role when viewing products on the frontend.

Related Blogs

Hide Products Based On Custom Field / ACF Value : WooCommerce

To hide products in WooCommerce based on a custom field or Advanced Custom Fields (ACF) value, you’ll need to use some custom code. Specifically, you’ll have to hook into the WooCommerce product query and modify it to include your custom field or ACF value as a condition for hiding products.

Ajax Add to Cart Quantity on Shop WooCommerce

To enable AJAX add to cart with quantity selectors on the WooCommerce shop page, you’ll need to use JavaScript to handle the AJAX request and update the cart quantity dynamically. Below are the steps to achieve this:

How to add custom order status in WooCommerce

To add a custom order status and customize its email template using `register_post_status`, `wc_order_statuses`, `woocommerce_email_actions`, and `woocommerce_email_classes`, you can follow these steps:

Request A Quote

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.