skip to Main Content

in the online store, I want to introduce a % discount for a specific group of customers.

I have already created a new group and customers and added the code below, which gives a discount to the cart.

/* discount biblioteka */
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit
    
    // only for 'biblioteka' role
    if ( ! current_user_can('biblioteka') )
        return; // Exit

    // Kortings percentage
    $percentage = 40;
    
    $discount = $cart->get_subtotal() * $percentage / 100; // Calculation
    
    // Applying discount
    $cart->add_fee( sprintf( __("biblioteka discount (%s)", "woocommerce"), $percentage . '%'), - $discount, true );
}

I would like this discount to also be displayed in the store as a promotional price and I added the code below, which made the website stop working.


/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)

function custom_price_assign( $price, $product ) {
    // Check if the user has a role of wholesaler
    if ( current_user_can('biblioteka') ){
        return $price * 0.40;
    }
    return $price;
}

What’s the simplest way I can display the regular price and the 40% off price as a promotional price for a special group of users?

2

Answers


  1. Chosen as BEST ANSWER

    In the end, I only stuck with the following

    /* Custom prices by user role */
    add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
    add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)
    
    function custom_price_assign( $price, $product ) {
        // Check if the user has a role of wholesaler
        if ( current_user_can('biblioteka') && $price > 0 ){
            $percentage = 40;
            return $price - ( $price * $percentage / 100 );
    
        }
        return $price;
    }
    

    I have a question about how I can limit the promotional price. Since some of the products are on sale, users with the "Library" role have a double discount.

    I would like a user with the "Library" role to be provided with a permanent 40% discount on the regular price, regardless of whether the product is in support.


  2. Try the following revised code instead, that will display the discounted product price in shop and single product for :

    // Conditional function: Targeting specific user role
    function is_biblioteka_role() {
        global $current_user;
        return in_array( 'biblioteka', $current_user->roles );
    }
    
    // Display discounted product price in shop and single product.
    add_filter( 'woocommerce_get_price_html', 'user_role_product_discounted_price_html', 20, 2 );
    function user_role_product_discounted_price_html( $price_html, $product ) {
        // Targeting specific user role
        if ( ! is_biblioteka_role() ) return $price_html;
    
        $percentage = 40; // Kortings percentage
        $price = $product->get_price();
    
        // Only for prices greater than zero
        if ( ! ($price > 0) ) return $price_html;
    
        $regular_price    = wc_get_price_to_display( $product );
        $discounted_price = wc_get_price_to_display( $product, array( 'price' => ($price * (100 - $percentage) / 100) ) );
        return wc_format_sale_price( $regular_price, $discounted_price ) . $product->get_price_suffix();
    }
    
    // Cart subtotal discount
    add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
    function discount_based_on_user_role( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Exit
        if ( ! is_biblioteka_role() ) return; // Targeting specific user role
    
        $percentage = 40; // Kortings percentage
        $cart->add_fee(
            sprintf( __("Biblioteka discount (%s)", "woocommerce"), $percentage.'%'), 
            -($cart->get_subtotal() * $percentage / 100), true 
        );
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search