mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/66606420/remove-all-taxes-in-woocommerce-for-a-min-cart-total-and-specific-countries/66607746
31 lines
1.3 KiB
Text
31 lines
1.3 KiB
Text
add_action( 'woocommerce_calculate_totals', 'set_customer_tax_exempt' );
|
|
function set_customer_tax_exempt( $cart ) {
|
|
if ( is_admin() && ! defined('DOING_AJAX') )
|
|
return;
|
|
|
|
$min_amount = 150; // Minimal cart amount
|
|
$countries = array('GB'); // Defined countries array
|
|
|
|
$subtotal = floatval( $cart->cart_contents_total );
|
|
$shipping = floatval( $cart->shipping_total );
|
|
$fees = floatval( $cart->fee_total );
|
|
$total = $subtotal + $shipping + $fees; // cart total (without taxes including shipping and fees)
|
|
|
|
$country = WC()->customer->get_shipping_country();
|
|
$country = empty($country) ? WC()->customer->get_billing_country() : $country;
|
|
$vat_exempt = WC()->customer->is_vat_exempt();
|
|
$condition = in_array( $country, $countries ) && $total >= $min_amount;
|
|
|
|
if ( $condition && ! $vat_exempt ) {
|
|
WC()->customer->set_is_vat_exempt( true ); // Set customer tax exempt
|
|
} elseif ( ! $condition && $vat_exempt ) {
|
|
WC()->customer->set_is_vat_exempt( false ); // Remove customer tax exempt
|
|
}
|
|
}
|
|
|
|
add_action( 'woocommerce_thankyou', 'remove_customer_tax_exempt' );
|
|
function remove_customer_tax_exempt( $order_id ) {
|
|
if ( WC()->customer->is_vat_exempt() ) {
|
|
WC()->customer->set_is_vat_exempt( false );
|
|
}
|
|
}
|