2

can anyone please fix this issue this snippet works fine but it have an issue,

the problem is when you add products from (categroie-1) and other category on the same cart the discount apply also for that category too, is it possible if user add also the other category in cart the discount apply just for the categorie-1 not for the other items of the cart?

Ex: categorie-1 25x products 50% discount categorie-2 2x prodcuts no discount

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;

// 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'] ) ) {
        // Quantity
        $product_quantity = $cart_item['quantity'];

        // Add to total
        $current_quantity += $product_quantity;
    }
}

// Greater than or equal to
if ( $current_quantity >= $minimun_quantity ) {
    // Calculation
    $discount = $cart->get_subtotal() * $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 );

jahn
  • 57
  • 7

0 Answers0