'Woocommerce payment error after discount calculation. Amount does not match total amount from the order lines

I am using the following custom snippet, which gives our customers a discount. If a customer orders 11 products (within the array), they get 1 free, so they only have to pay for 10. If a customer orders 22 products, they get 2 products for free and so on.

This is the code:

add_action('woocommerce_cart_calculate_fees', 'buy_ten_get_one_free', 10, 1 );
function buy_ten_get_one_free( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Set HERE your targeted variable products IDs
    $targeted_product_ids = array(5341, 4435, 4434, 4433, 4431, 4430, 4429, 3849, 3848, 3847, 245);

    $each_n_items = 11; // Number of items required to get a free one
    $discount = 0; // Initializing
    $items_prices = array(); // Initializing 

    foreach ( $cart->get_cart() as $cart_item ) {
        if( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
            $qty = intval( $cart_item['quantity'] );

            for ( $i = 0; $i < $qty; $i++ ) {
                $items_prices[] = floatval( $cart_item['data']->get_price() );
            }
        }
    }
    $count_items_prices = count($items_prices);

    if ( $count_items_prices >= $each_n_items ) {
        foreach ( $items_prices as $key => $price ) {
            
            
            $price_excl_tax = ($price / 109) * 100;
            
            
            //if ( $key % ($each_n_items + 0) == 1 ) {
            if ( $key % ($each_n_items + 1) == 1 ) {
            //if ( ( $key + 1 ) % $each_n_items == 0 ) {
                $discount += number_format($price_excl_tax, 4 );
                
            }
        }
    }
    
    

    if ( $discount > 0 ) {
        // Displaying a custom notice (optional)
        wc_clear_notices();
        //wc_add_notice( __("10 +1 free product"), 'notice');

        // The discount
        $cart->add_fee( __("10 +1 free product"), -$discount, true  );
    }
}

This part is working. The cart shows the correct discount and correct total amount.

After a customer wants to pay for an order, i receive an error in our logfiles:

'Error executing API call (422: Unprocessable Entity): The amount of the order does not match the total amount from the order lines. Expected order amount to be €18.15 but got €18.00. Field: amount. Documentation: https://docs.mollie.com/overview/handling-errors'

The prices shown in the message above are calculated as follow: €18, which is correct -> 11 products x 1.80 (per product) = 19.80 - 1 free products = €18

The €18.15 is incorrect. It looks like the order amount is forgetting to subtract the tax (9% is equal to 0.15 cents) from the free product. If the customer orders 22 products, the price difference is .30 cents; so this theory seems to be correct.

There's no error when a customer orders < 11 products.

Is there a problem with the custom snippet?



Sources

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

Source: Stack Overflow

Solution Source