0

So, I am working with my custom plugin in which I need to get zip code or postcode on woocommerce checkout page and add my additional price and be done with the payment. I tried using AJAX, which get the zip code and add my price into it and update the price.

/**
Here my AJAX Call
**/
global $woocommerce;
//$additional_amount is coming dynamically via an external API
$additional_amount = 5.00; // can vary depending upon zip code
number_format((float)$additional_amount, 2, '.', '');
$woocommerce->cart->set_total($woocommerce->cart->total + $additional_amount);

Then I send this total to my AJAX call back like below:

$arr['amount'] = $woocommerce->cart->get_total();
echo json_encode($arr);exit();

It shows me updated price but when I do payment process using stripe or any other payment gateway, it charges my the old price.

I also tried it this way

$fee = 5.00;
$woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );

But it also does not work. Is there a way to do add my additional price into checkout amount using AJAX or any other way. I am using Wordpress 5.4 and Woocommerce version 4.0.1

2
  • Shouldn’t this just be set up as the shipping price? Why are you doing this with code? – Jacob Peattie Apr 18 '20 at 16:31
  • Well, this is not about shipping address, I have an API which sends me Tax information according to zip code and there is no place in the backend to add this API in the backend. – Husnain Ahmed Apr 18 '20 at 16:40
0

As I was trying to add some additional amount to my charge and I was unable to update the charge amount. The code which I was trying was this using AJAX call:

$woocommerce->cart->set_total($woocommerce->cart->total + $additional_amount);

But the real problem was with my payment method, not the cart price. So, I resolved it using a filter which happens just before charge amount is about to sent to database and payment gateway. The code which works for me is this:

function additional_amount($order){
    $total_additional_amount = $amount1 + $amount 2;
    $total_additional_amount = round($total_additional_amount, 2);
    $order->set_total( $total_amount);
}
add_filter( 'woocommerce_checkout_create_order', 'additional_amount', 10, 1 );

Using this filter, I was able to update the actual charge amount. I hope that will help to someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.