0

Here is what we are trying to achieve:

Condition A: If user is from ZA, then the price should be X + 15% of X = 1500

Condition B: If not ZA, then it should be just 1500

The calculation on the checkout page is fine, but when I then pay, it automatically adds appr. 13% on top of the amount which makes the calculation, invoice and transaction incorrect.

This is a custom Tax Calculation. The product is added without Tax in the backend at 1500. Global Tax Settings are disabled.

add_action('woocommerce_checkout_update_order_review', 'armd_update_order_review');

function armd_update_order_review( $posted_data ) {

    // Parsing posted data on checkout    
    $post = array();    
    $vars = explode('&', $posted_data);    
    foreach ($vars as $k => $value){

        $v = explode('=', urldecode($value));    
        $post[$v[0]] = $v[1];    
    }

    $billing_country = $post['billing_country'];    
    if ($billing_country == "ZA") {    
        $tax = 0; $sub_total = 0;    
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            $product = $cart_item['data'];

            $price = $cart_item['data']->get_price();

            $percentage = 15;

            $new_price = ($price*100)/(100+$percentage);

            $sub_total += $new_price;

            $tax += ($price - $new_price)*$cart_item['quantity'];

            $cart_item['data']->set_price( floatval($new_price) );

        }

        WC()->session->set( 'tax' , $tax );

        WC()->session->set( 'sub_total' , $sub_total );

    }else{

        WC()->session->__unset( 'tax' );

        WC()->session->__unset( 'sub_total' );

    }

}

add_action('woocommerce_cart_calculate_fees','armd_wc_custom_fee');

function armd_wc_custom_fee() {

    if(!is_checkout())return false;

    $billing_country = WC()->customer->get_billing_country();

    $tax = WC()->session->get( 'tax' );

    if($billing_country == 'ZA' && !empty($tax)){

        WC()->cart->add_fee('VAT @ 15%:', $tax);

    }

}

What are we overseeing here? Again, the calculation in the cart and checkout is correct, just then incorrectly sent into payment gateway. We tested multiple payment gateways and COD, same issue.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.