WooCommerce - Override Shipping Cost - wordpress

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:)

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 - Tax optional

Is there a way for the customer to choose whether or not to include the tax in his order?
I imagine a field called "With / Without TAX"
And some code that based on that field, perform the necessary calculations.
If this is not possible, how could you add a button within the order edition to include or exclude taxes?
Thank you.
I did something similar by hooking into woocommerce_checkout_create_order when the order is placed. (Normal orders are taxed on customer's address, curbside orders are based on store address. You can simply use 0 based on if your customer checks a box)
add_action( 'woocommerce_checkout_create_order', array( $this, 'curbside_order'), 10, 1);
public function curbside_order( $order ) {
if( /** without tax - post_meta, checkbox or however you set it**/){
$order->set_cart_tax(0);
}
}

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

How to force remove products from WooCommerce cart?

I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.

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

Resources