'Discount for a specific product category in WooCommerce cart based on quantity and multiples

The discount I currently have essentially says

  • If 6 products within a specific category are in the cart --- discount the total price of $10

This piece of the code works fine. My issue is, this does not work

  • If there are 7 products, the 7th product being from a different category.

My goal is to give the discount no matter how many products are in the cart so long as there are 6 products within "Category A".

The following code works so long as there is 6 products in the "discounted category" or there is 1 product with a quantity of 6 etc. It falls apart when I add a product from another category. Feel free to rip it apart.

add_action( 'woocommerce_before_calculate_totals', 'this_item_free' );

function this_item_free() {

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $valid_product_category_id = array('soup-mix');
    $has_category = false;

    $count = WC()->cart->get_cart_contents_count();

    foreach ( WC()->cart->get_cart() as $product ) {
        $quantity = $product['quantity'];
        $price = $product['data']->get_price();
    }

    if ( has_term( $valid_product_category_id, 'product_cat', $product['product_id'],
            $product['quantity'] ) ) {
        $has_category = true;
        $cart_total = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) );

        if($count == 6 && $has_category = true){
            add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
            function discount_based_on_total( $cart ) {

                $total = $cart->cart_contents_total;
                $discount = 9.95;

                $cart->add_fee( __('discount', 'woocommerce'), -$discount );
                wc_add_notice( apply_filters( 'discount_applied', 'You just got a free soup!') );

            }
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source