Implementing Different Prices for User Roles in WooCommerce

Implementing Different Prices for User Roles in WooCommerce

To establish distinct prices for wholesalers, resellers, and business customers in WooCommerce, you can proceed with the following steps:

 

Step 1: Create User Roles

Begin by creating user roles tailored for wholesalers, resellers, and business customers. You can achieve this by employing a plugin such as “Members” or “User Role Editor.” Alternatively, you can directly integrate the following code into your theme’s functions.php file to create the necessary 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

Allocate the respective user roles to your wholesalers, resellers, and business customers through the WordPress admin panel.

 

Step 3: Configure Different Prices for Each User Role

Now, customize the pricing structure for each user role. Utilize the woocommerce_product_get_price filter hook for this purpose. Embed the following code snippet into your theme’s functions.php file or create 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: Integrate Meta Boxes for Custom Prices

Integrate meta boxes to facilitate the input of custom prices for each user role. Use the add_meta_boxes action hook to create the meta boxes and the 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 outlined steps, you can now effectively implement varying prices for wholesalers, resellers, and business customers within WooCommerce, utilizing meta boxes. These customized prices will be prominently displayed based on the designated user roles when viewing products on the frontend.

Leave a Comment

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