13

I'm looking to display a different price based on a user role (wholesaler, dealer, etc) AND based on the category.

There's a dynamic pricing plugin that displays these discounts once an item is added to the cart, but not on the page itself.

Is there a way to use a filter or action to check for the user level, check the item's category and then change the price dynamically?

closed as off-topic by Pieter Goosen, birgire, Johannes Pille, Wyck, mrwweb Sep 8 '14 at 4:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

18

Yes there is, you can use the woocommerce_get_price filter hook to filter the value based on user role and return a price accordingly e.g:

add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2);
/**
 * custom_price_WPA111772 
 *
 * filter the price based on category and user role
 * @param  $price   
 * @param  $product 
 * @return 
 */
function custom_price_WPA111772($price, $product) {
    if (!is_user_logged_in()) return $price;

    //check if the product is in a category you want, let say shirts
    if( has_term( 'shirts', 'product_cat' ,$product->ID) ) {
        //check if the user has a role of dealer using a helper function, see bellow
        if (has_role_WPA111772('dealer')){
            //give user 10% of
            $price = $price * 0.9;
        }
    }
    return $price;
}

/**
 * has_role_WPA111772 
 *
 * function to check if a user has a specific role
 * 
 * @param  string  $role    role to check against 
 * @param  int  $user_id    user id
 * @return boolean
 */
function has_role_WPA111772($role = '',$user_id = null){
    if ( is_numeric( $user_id ) )
        $user = get_user_by( 'id',$user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
        return false;

    return in_array( $role, (array) $user->roles );
}
  • I tried this. Added this code in functions.php. Doesn't work. – fuddin Jun 18 '16 at 15:12
  • 1
    Thanks for this great answer. For some reason my variation prices do not change using this filter. Are you familiar with this problem? – Robbert Jul 11 '18 at 12:13
  • Works only on Simple products – Klevis Miho Oct 1 '18 at 18:08
0

You can try out Customer Specific Pricing for WooCommerce. With this plugin, you can add different prices for registered users.

Currently the plugin is in its initial phase, but there would be updates soon to support additional features like price based on product category.

  • this plugin can only discount a product. What if you want to raise the price? – Klevis Miho Oct 1 '18 at 18:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.