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

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.

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;
}

Related

Changing shipping cost in Woocommerce

I am creating a plugin that connects to an 3rd API to request the shipping cost based on the address of the person and the dimensions of the product.
Everything is fine but now I need the values ​​returned by the API to be added to the prices of the shipping methods and I tried with this code but it still doesn't work.
add_filter('woocommerce_package_rates','test_overwrite_fedex',100,2);
function test_overwrite_($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = $phpVar1; <-//variable that receives the value of the shipment
}
return $rates;
But I don't find any way to change the shipping cost with functions..
Is it possible ? Thanks

Woocommerce Checkout - "State" field is not showing for certain countries - causing payment errors

"state" field in woocommerce checkout page is not appearing when choosing certain countries like germany, so when the customer clicks place order, and error appears that he needs to put the state, however its not possible to put the state since there is no field for it.
1- if he chooses countries like United Arab Emirates, USA, Canada, etc..., a state field appears and the customer can fill it normally and the payment goes through with no problems.
2- if Germany, or Afghanistan or some different countries are chosen, the "state/county" field disappears, and when the customer clicks "place order" and error appears: "shipping_state post variable not found".
i'm using paytabs as a payment gateway.
I'm not sure what code to edit, but woocommerce shows "state" fields dynamically based on chosen country.
error appears that the customer needs to put the state, however its not possible to put the state since there is no field for it.
As default woocommerce does not show state field for some countries you can see those countries in class-wc-countries.php
the below code will allow state field for Afghanistan please test and edit to your suiting
function so_57838961_filter_woocommerce_states( $states ) {
unset( $states['AF'] );
var_dump( $states ) ;
return $states;
};
add_filter( 'woocommerce_states', 'so_57838961_filter_woocommerce_states', 10, 1 );
function so_57838961_filter_woocommerce_get_country_locale( $locale ) {
$locale['AF']['state']['required'] = true;
return $locale;
};
add_filter( 'woocommerce_get_country_locale', 'so_57838961_filter_woocommerce_get_country_locale', 10, 1 );
The error you are experiencing is the line with the "var_dump", which is outputting all the states information to the screen. Just remove that line and it works perfectly, I just did this on my website with Austria "AT".
For more information, just google "php var_dump" and it tells you what that function does.

exclude "On Hold" orders from Woo Sales reports

I want to remove/exclude any orders with the order status "On Hold" from woocommerce sales reports. By default On hold orders are included in the sales reports.
There is a function "exclude_from_order_sales_reports" found here - https://docs.woothemes.com/wc-apidocs/function-wc_register_order_type.html - that will exclude a post type from sales reports.
Also the woo order types and order status are handled here - https://docs.woothemes.com/wc-apidocs/source-function-wc_register_order_type.html#167-221
Unfortunately my php coding is basic (im still learning) and i have no clue where to start with all this code. How can I use the above function to remove on hold order from sales reports?
i seen on woocommerce main report class WC_Admin_Report on line 67 they have filter like this $order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status );
so i think you can easily alter the default order status for reports by adding filter like this on your functions.php
add_filter( 'woocommerce_reports_order_statuses', 'my_custom_order_status_for_reports', 10, 1 );
function my_custom_order_status_for_reports($order_statuses){
$order_statuses = array('completed'); // your order statuses for reports
return $order_statuses;
}

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

WooCommerce - Override Shipping Cost

I am building an addon for WooCommerce that will allow the admin to set a per-customer flat rate shipping price. I am trying to hook into the function that calculates the shipping price so that I can override the calculated price and method and return only one shipping option with the pre-set price for that customer. What hook can I use to accomplish this?
I think it might be the woocommerce_calculated_shipping hook, but I can't find a good example of how to use it.
This is probably a late answer, but if someone else needs it:
add_filter('woocommerce_package_rates','test_overwrite_fedex',100,2);
function test_overwrite_fedex($rates,$package) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = 1000;
//Set the TAX
$rate->taxes[1] = 1000 * 0.2;
}
return $rates;
}
The rates are cached by Woocommerce using the wordpress transient function. So when you're testing, make sure you change the item quantity so the package rates are updated correctly, or you can empty the cart each time you refresh:)

Resources