php - How can I update the price when someone enters postcode or zip code in woocommerce checkout page?

admin2025-01-07  3

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

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

Share Improve this question asked Apr 18, 2020 at 13:48 Husnain AhmedHusnain Ahmed 437 bronze badges 2
  • Shouldn’t this just be set up as the shipping price? Why are you doing this with code? – Jacob Peattie Commented Apr 18, 2020 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 Commented Apr 18, 2020 at 16:40
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261148a718.html

最新回复(0)