1

I want to give discount for my customer based user role ex:

  • for user role (subscriber) 50% discount if they buy 25 products from a specific category.

If use a snippet but it is not cover the second part of my question (25 products from a specific category).

// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_on_user_role', 20, 1 );
function discount_on_user_role( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return; // Exit

    // Only for 'company' user role
    if ( ! current_user_can('company') )
        return; // Exit

    // HERE define the percentage discount
    $percentage = 50;

    $discount = $cart->get_subtotal() * $percentage / 100; // Calculation

    // Applying discount
    $cart->add_fee( sprintf( __("Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}

Any advice?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
jahn
  • 57
  • 7
  • You first have to conquer the issue of storing the fact that the user has purchased 25 products from the specific category. To do this, I'd consider hooking the `woocommerce_thankyou` hook and adding some user_meta as a counter. – Howard E Oct 31 '21 at 13:20

1 Answers1

2

For the category categorie-1 (adjust if desired) you can use a counter based on the product quantity in the cart.

When this is equal to 25 or more you can apply the discount.

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Only for 'company' user role
    if ( ! current_user_can( 'company' ) )
        return;
    
    // Category
    $category = 'categorie-1';

    // Percentage discount
    $percentage = 50; // 50%
    
    // Min quantity
    $minimun_quantity = 25;
    
    // Initialize
    $current_quantity = 0;
    $current_total = 0;

    // Loop though each cart item
    foreach ( $cart->get_cart() as $cart_item ) {
        // Has certain category     
        if ( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
            // Get quantity
            $product_quantity = $cart_item['quantity'];

            // Add to quantity total
            $current_quantity += $product_quantity;
            
            // Get price
            $product_price = $cart_item['data']->get_price();
            
            // Add to current total
            $current_total += $product_price * $product_quantity;
        }
    }

    // Greater than or equal to
    if ( $current_quantity >= $minimun_quantity ) {
        // Calculation
        $discount = $current_total * $percentage / 100;

        // Applying discount
        $cart->add_fee( sprintf( __( 'Discount (%s)', 'woocommerce' ), $percentage . '%'), -$discount, true );     
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • hi Thanks for the update, but now the calculation is not correct it shows like this products prices is 19.99 x 25 = 499,75 and 50% discount is 297.36 but correct is 50% discount is 249,87 – jahn Nov 08 '21 at 07:59
  • @jahn The calculation is correct for me. The calculation goes like this: 499.75 * 50 / 100. What you could try is to replace `$cart_item['data']->get_price();` with `$cart_item['data']->get_regular_price();`. if that doesn't fix the new issue either, it's best to [debug the process step by step](https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3) – 7uc1f3r Nov 08 '21 at 08:10
  • but in my shop the tax is (Yes, I will enter prices inclusive of tax) – jahn Nov 08 '21 at 08:16
  • I replace with wc_get_price_including_tax but now cart shows e´this error (Fatal error : Uncaught Error: Call to undefined method WC_Product_Simple::wc_get_price_including_tax() in – jahn Nov 09 '21 at 11:20
  • @jahn if you want to use that specific function you can replace `$product_price = $cart_item['data']->get_price();` with `$product_price = (float) wc_get_price_including_tax( $cart_item['data'] );` – 7uc1f3r Nov 09 '21 at 11:26