user2390733
user2390733

Reputation: 509

How to show correct tax on basket (BEFORE checkout)?

I have a simple problem.

I have set up multiple tax rates for different countries. However, on the basket page - when not having visited the checkout page so far - it shows the tax from the base country.

In my case: I have a shop based in AT. I have set up taxes for AT and CH. If a user visits with a Switzerland IP, I restrict the country list to just Switzerland, and set a PHP variable. Nevertheless the country isn't in the woocommerce_countries anymore, WC calculates the taxes with the base country tax setting.

See those images:

taxes in basket - taxes on checkout

I want to show the correct tax BEFORE the checkout. I already figured out that the correct taxes are shown when the user has chosen a country on the checkout page, and a "$woocommerce->customer" node is available. But I struggle to make that happen.

Anyone got an idea how to do this? Here's my plugin code which does not work:

define('USERCOUNTRY', get_country_proper()); // returns 'CH'
$customer = new WC_Customer();
WC()->customer->set_country(USERCOUNTRY);

Result:

Fatal error: Call to a member function get() on a non-object in wp-content/plugins/woocommerce/includes/class-wc-customer.php on line 27

Update: The tax which will be used on the CART page (before entering a country on the CHECKOUT) is used here:

Woocommerce -> Settings -> Tax -> Default Customer Address: [Shop Base country | none] http://docs.woothemes.com/document/setting-up-taxes-in-woocommerce/

OK, can I alter this via script?

Thanks for any help.

Upvotes: 0

Views: 11359

Answers (1)

user2390733
user2390733

Reputation: 509

There is, actually, a hook for that.

// this is used for taxing:
add_filter('woocommerce_countries_base_country', 'set_base_to_usercountry', 1, 1);

// and this is used for shipping:
add_filter('woocommerce_customer_default_location', 'set_base_to_usercountry', 1, 1);

function set_base_to_usercountry($country) {
    $country = USERCOUNTRY; // comes from a geoIP lookup in my case.
    return $country;
}

// and this is also needed not to have trouble with the "modded_tax".
// (which looks like rounding issues, but is a tax conversion issue.)
add_filter('woocommerce_customer_taxable_address', 'alter_taxable_address', 1, 1);
function alter_taxable_address($address) {
    // $address comes as an array with 4 elements. 
    // first element keeps the 2-digit country code.
    $address[0] = USERCOUNTRY; 
    return $address;
}

Upvotes: 1

Related Questions