Search code examples
phpwordpresswoocommercecartdiscount

Progressive discount based on cart total and user roles in WooCommerce


I'm using Progressive discount based on cart total in WooCommerce answer code to make some Woocommerce order total discounts (see below). But I would like to make a discount based on user role, as each of my customer roles sees different prices.

I have some custom user roles: wholesale_prices, wholesale_vat_exc, and distributor_prices.

I want to make the code to work just for wholesale_prices and wholesale_vat_exc user roles, but not for distributor_prices (as they must not see the discounts).

Here is my actual revisited code version:

// Order total discount

add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent != 0 ) {
        $discount =  $cart_total * $percent / 100;
        $cart_object->add_fee( "Bulk Order Discount ($percent%)", -$discount, true );
    }
}

How to make this code only available to wholesale_prices and wholesale_vat_exc user roles?


Solution

  • As you can see on Apply a discount for a specific user role in Woocommerce related answer, I use the WordPress conditional function current_user_can() to target a specific user role…

    So you need to use it in Progressive discount based on cart total in WooCommerce answer code just like:

    add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
    function progressive_discount_based_on_cart_total( $cart ) {
        // HERE we target other user roles than 'distributor_prices' (allowing guests)
        if ( current_user_can('distributor_prices') && is_user_logged_in() )
            return;
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $cart_total = $cart->get_cart_contents_total(); // Cart total
    
        if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
            $percent = 15; // 15%
        elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
            $percent = 10; // 10%
        elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
            $percent =  5; // 5%
        else
            $percent = 0;
    
        if ( $percent > 0 ) {
            $discount   = $cart_total * $percent / 100;
            $label_text = sprintf( __("Bulk Order Discount %s"), '('.$percent.'%)' );
            $cart->add_fee( $label_text, -$discount, true );
        }
    }
    

    Code goes on functions.php file of your active child theme (or active theme). It should works.


    For multiple user roles you will use wp_get_current_user() to get the current WP_User Object, and then you can get the roles property like:

    $user       = wp_get_current_user();
    $user_roles = $user->roles;  // An array of the user roles
    

    Then you will replace in the code:

        // HERE we target other user roles than 'distributor_prices' (allowing guests)
        if ( current_user_can('distributor_prices') && is_user_logged_in() )
            return;
    

    by:

        // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
        if ( ! array_intersect( wp_get_current_user()->roles, array('wholesale_prices', 'wholesale_vat_exc') && is_user_logged_in() )
            return;
    

    Or

        // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
        if ( ! ( current_user_can('wholesale_prices') || current_user_can('wholesale_vat_exc') ) && is_user_logged_in() )
            return;