class CheckoutTax
{
public function __construct()
{
// add a note after the order details, when a tax is charged and country changes
// the html snippet is shown after the order details on a page refresh
add_action('woocommerce_review_order_before_payment', array($this, 'checkout_note_tax'));
// the action is called when the country changes
// but it does not show the html snippet at the correct place
add_action('woocommerce_cart_calculate_fees', array($this, 'checkout_note_tax'));
// how to combine
// - show HTML snippet after the order details
// - act on a country change
}
/**
* Show checkout note, tax paid
*
* @return void
*/
function checkout_note_tax() {
if (is_admin() && !defined('DOING_AJAX')) return;
if (!is_checkout()) return;
global $WOOCS;
$total_tax = WC()->cart->get_totals()['total_tax'];
$shipping_country = WC()->customer->get_shipping_country();
error_log( $total_tax );
error_log( $shipping_country );
if( $total_tax > 0 && $shipping_country != 'CH') {
?>
<h3>VAT / Custom Duties</h3>
<?php
}
}
}
The hook woocommerce_review_order_before_payment shows the html snippet at the correct page, but only on a page refresh.
The hook woocommerce_cart_calculate_fees triggers changes made to the country field.
How can I combine an action to show a HTML snippet after the order details on the checkout page and update the HTML snippet when the shipping country is changed?
2
Answers
Here is another solution, although it will show in the order table before the total. (I used something similar to add checkboxes for shipping insurance and calculating the insurance fee).
You need something a bit different to get your custom content refreshed on
checkout_update
Ajax event. I have replacedwoocommerce_review_order_before_payment
hook with a more convenient one.Now your content is also added to Ajax "order review fragments", so on "Update checkout" event your display gets refreshed dynamically.
Try the following (commented):
For testing purpose in the child theme’s functions.php file, I use additionally:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Related: