Pawan Kumawat

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.

Categories

Related Blogs

How to Remove Product Tabs on Product Page

To remove product tabs on the WooCommerce product page, you can use the `woocommerce_product_tabs` filter. This filter allows you to modify or remove the default product tabs provided by WooCommerce. By removing the tabs you don’t want, you can customize the product page layout to suit your needs.

Jquery Form Validator

JQuery Form Validator is a featured jQuery plugin that makes it easy to validate user input while keeping your HTML clean from javascript code.