• Resolved ComMotion

    (@werbeagenturcommotion)


    Hi guys,

    I have a custom code which adds custom fees in the cart automatically. When the customer uses a coupon the coupon gets applied before the fee gets applied. Is there any possibility to apply the coupon after the fee gets applied? Thanks in advance.

    Here is my code so far:

    // Buy 10 get 1 free
    function buy_10_get_1_free( WC_Cart $cart ) {
    	$cart_contents_count = $cart->cart_contents_count;
    	if ( $cart_contents_count < 11 ) return; // free tape only for the 11th product and up
    
    	$cart_contents_count = 0;
    	foreach ( $cart->get_cart() as $cart_item_key => $values ) {
    		$product = $values['data'];
    		$quantity = $values['quantity'];
    
    		if ( has_term( 'tapes', 'product_cat', $product->id ) || has_term( 'tape', 'product_cat', $product->id ) ) {
    			$cart_contents_count = $cart_contents_count + $quantity;
    
    			// $product_price[] = $_product->get_price();
    			$product_price[] = wc_get_price_excluding_tax( $product );
    		}
    	}
    
    	$cheapest = min( $product_price );
    
    	if ( $cart_contents_count >= 89 ) {
    		$cart->add_fee( __( 'Gratis Tapes (75+14)', 'woocommerce' ), -($cheapest * 14) );
    	} else if ( $cart_contents_count >= 83 ) {
    		$cart->add_fee( __( 'Gratis Tapes (70+13)', 'woocommerce' ), -($cheapest * 13) );
    	} else if ( $cart_contents_count >= 77 ) {
    		$cart->add_fee( __( 'Gratis Tapes (65+12)', 'woocommerce' ), -($cheapest * 12) );
    	} else if ( $cart_contents_count >= 71 ) {
    		$cart->add_fee( __( 'Gratis Tapes (60+11)', 'woocommerce' ), -($cheapest * 11) );
    	} else if ( $cart_contents_count >= 65 ) {
    		$cart->add_fee( __( 'Gratis Tapes (55+10)', 'woocommerce' ), -($cheapest * 10) );
    	} else if ( $cart_contents_count >= 59 ) {
    		$cart->add_fee( __( 'Gratis Tapes (50+9)', 'woocommerce' ), -($cheapest * 9) );
    	} else if ( $cart_contents_count >= 53 ) {
    		$cart->add_fee( __( 'Gratis Tapes (45+8)', 'woocommerce' ), -($cheapest * 8) );
    	} else if ( $cart_contents_count >= 47 ) {
    		$cart->add_fee( __( 'Gratis Tapes (40+7)', 'woocommerce' ), -($cheapest * 7) );
    	} else if ( $cart_contents_count >= 41 ) {
    		$cart->add_fee( __( 'Gratis Tapes (35+6)', 'woocommerce' ), -($cheapest * 6) );
    	} else if ( $cart_contents_count >= 35 ) {
    		$cart->add_fee( __( 'Gratis Tapes (30+5)', 'woocommerce' ), -($cheapest * 5) );
    	} else if ( $cart_contents_count >= 29 ) {
    		$cart->add_fee( __( 'Gratis Tapes (25+4)', 'woocommerce' ), -($cheapest * 4) );
    	} else if ( $cart_contents_count >= 23 ) {
    		$cart->add_fee( __( 'Gratis Tapes (20+3)', 'woocommerce' ), -($cheapest * 3) );
    	} else if ( $cart_contents_count >= 17 ) {
    		$cart->add_fee( __( 'Gratis Tapes (15+2)', 'woocommerce' ), -($cheapest * 2) );
    	} else if ( $cart_contents_count >= 11 ) {
    		$cart->add_fee( __( 'Gratis Tape (10+1)', 'woocommerce' ), -$cheapest );
    	}
    }
    add_action( 'woocommerce_cart_calculate_fees', 'buy_10_get_1_free' );
    • This topic was modified 3 years, 4 months ago by ComMotion.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce add fee before coupon’ is closed to new replies.