Hi have a wocommerce plugin, While changing the payment method. the cost for shipping my function calculate_shipping is not being called. and therefore shipping methods are not updating with appropriate cost
here are some samples from my code.
public function __construct() {
add_action('woocommerce_review_order_before_payment', array($this, 'update_shipping_charges'), 1);
}
public function update_shipping_charges() {
// jQuery code
?>
<script type="text/javascript">
(function ($) {
$('form.checkout').on('change', 'input[name^="payment_method"]', function () {
// Set the select value in a variable
$('body').trigger('update_checkout');
//testing
$('body').on('updated_checkout', function() {
console.log('updated');
});
});
})(jQuery);
</script>
<?php
}
2
Answers
Try wrapping everything inside the init hook. Maybe the code gets executed too early:
You could try multiple hooks if
init
don’t works:Please try the following (Add to functions.php or via Code Snippets plugin)
A couple notes….
wc-checkout
script is loaded.payment_method_selected
(but yes, it’s a core Woo trigger in the checkout) trigger which should fire when the payment method is changedupdate_checkout
function.Don’t have any shipping methods set up locally to test it properly, but I hope it points you in the right direction.