简体   繁体   中英

woocommerce should show same price for eu but net worth for other countries

I've been looking for a solution for two days and unfortunately I can't find it. Woocommerce is supposed to display the same gross price for EU citizens including the usual national tax. For third countries outside the EU, however, the net price without tax should be displayed.

So if I use the filter add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false'); in general, it works within the EU, outside it shows the gross price.

So I thought I'd first try to determine the country in the checkout and via functions.php the filter is either activated or deactivated:

add_action( 'woocommerce_cart_calculate_fees', 'same_price_for_eu' );
function same_price_for_eu(){

   $countries = new WC_Countries();
  
   $eu_countries = $countries->get_european_union_countries();

   $billing_country = get_option( 'billing_country' );
   $shipping_country = get_option( 'shipping_country' );

   if ( in_array( $shipping_country, $eu_countries ) ) {
  add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false');
    
   } else {
   remove_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false');
     
   }
}

Surely you can help me, as I have no idea what I'm doing:-)


Edit: Could solve it!
 add_action( 'woocommerce_cart_calculate_fees', 'same_price_for_eu' ); function same_price_for_eu(){ $countries = new WC_Countries(); $eu_countries = $countries->get_european_union_countries(); if ( in_array( WC()->customer->get_shipping_country(), $eu_countries) ){ add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false'); } else { remove_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false'); } }

your solution is almost correct aka. the tax calculation was still off. i checked the cart, when changing the country the tax calculation was wrong. but thanks to your efforts i created this proper working solution:

add_action('woocommerce_adjust_non_base_location_prices', 'so67632352_woocommerce_adjust_non_base_location_prices');

function so67632352_woocommerce_adjust_non_base_location_prices()
{
    $countries = new WC_Countries();
    $eu_countries = $countries->get_european_union_countries();

    if (in_array(WC()->customer->get_shipping_country(), $eu_countries)) {
        return false;
    } else {
        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM