I've set up a woocommerce store with multiple users (B2C & B2B). Some of them will automatically be exempt from tax and just have tax disappear from the cart/checkout. I've used a dynamic pricing plugin to provide different prices to different roles but there is no options for tax variations.
I found this answer and tried to put it in place Role based taxes in woocommerce but as #Jplus2 is telling, #dryan144 solution is not good because it is only applied during the checkout and not on the cart. I tried to figure out the way to do it but I still do have to refresh my 'cart' page to display taxes to 0 (as they are included in the price for "guest" or "customer", any help to launch the action when my cart page is called?
I did the following:
add_filter( 'woocommerce_before_cart_contents', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_shipping_calculator', 'prevent_wholesaler_taxes' );
add_filter( 'woocommerce_before_checkout_billing_form', 'prevent_wholesaler_taxes' );
function prevent_wholesaler_taxes() {
global $woocommerce;
if ( is_user_logged_in() && !(current_user_can('customer'))){
$woocommerce->customer->set_is_vat_exempt(false);
} else {
$woocommerce->customer->set_is_vat_exempt(true);
}
} //end prevent_wholesaler_taxes
It's working straight away sometimes but most of the time it's only after working after a refresh of my cart which is not good.
Try to add https://eshoes.com.au/product/test-shoes08/ to the cart then -> View your basket
Any help would be greately appreciated ;)
Cheers
This solution works perfectly, instead of using set_is_vat_exempt() I simply used $tax)class = 'Zero Rate':
add_filter( 'woocommerce_before_cart_contents', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_before_shipping_calculator', 'wc_diff_rate_for_user', 1, 2);
add_filter( 'woocommerce_before_checkout_billing_form', 'wc_diff_rate_for_user', 1, 2 );
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );
function wc_diff_rate_for_user( $tax_class ) {
if ( !is_user_logged_in() || current_user_can( 'customer' ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
}
Related
I'm customizing Woocommerce plugin, trying to remove decimals from product variation prices in product detail page.
Ex- if someone select option i want to display the regular and sales prices without decimals and the other locations of site like shop, cart, checkout no need to change.
I found this filter but it change prices of whole site.
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );
https://snipboard.io/GXgtUi.jpg
anyone have idea or solution about archive this, Thanks a lot
You can try below code snippet into your theme function.php file
add_filter( 'formatted_woocommerce_price', 'ums_remove_zero_decimals', 10, 5 );
function ums_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
if( is_single()) {
if($product->is_type( 'variable' )) {
return (int) round( $price );
}
else {
return ( $formatted_price);
}
}
}
Let me know if this works for you or not.
I would like Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another one is added then it should remove the previous one.
I found this code on net:
/**
* When an item is added to the cart, remove other products
*/
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart, please remove previous
item.', 'error' ); // here instead popup notice need to remove prevous added product
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart',
10, 3 );
But it seems that is not working as I wanted. It need to autoremove previously added product in cart, and add latest added product in cart. Can someone to give me tip what need to update on class to work in latest woo 3.3.X ?
This one is the more compact and actual code as global $woocommerce is not needed anymore:
add_filter( 'woocommerce_add_to_cart_validation', 'auto_empty_cart', 20, 3 );
function auto_empty_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->is_empty() ) return $passed; // Exit
WC()->cart->empty_cart();
return $passed;
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works in all Woocommerce versions since 2.6.x
This working perfecty on Woocommerce 3.3.X
add_filter( 'woocommerce_add_to_cart_validation',
'bbloomer_only_one_in_cart', 99, 2 );
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
global $woocommerce;
// empty cart: new item will replace previous
$woocommerce->cart->empty_cart();
// display a message if you like
wc_add_notice( 'Product added to cart!', 'notice' );
return $passed;
}
I need to double the price for every product in Woocommerce frontend. For this I used the following code:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
But there is an error using this code. checkout page price is not correct.
for example the product orginal price is 10. We double the price by this code . so the product price is 20 now. When i added this product to the cart then cart and checkout page price is 40. That means this multiplication is take place in two times.
Please help to solve this.
Updated To double the price:
1) First you will restrict your code to single product and archives pages only:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
if( is_shop() || is_product_category() || is_product_tag() || is_product() )
return $price*2;
return $price;
}
2) Then for cart and checkout pages, you will change the cart item price this way:
add_filter( 'woocommerce_add_cart_item', 'set_custom_cart_item_prices', 20, 2 );
function set_custom_cart_item_prices( $cart_data, $cart_item_key ) {
// Price calculation
$new_price = $cart_data['data']->get_price() * 2;
// Set and register the new calculated price
$cart_data['data']->set_price( $new_price );
$cart_data['new_price'] = $new_price;
return $cart_data;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'set_custom_cart_item_prices_from_session', 20, 3 );
function set_custom_cart_item_prices_from_session( $session_data, $values, $key ) {
if ( ! isset( $session_data['new_price'] ) || empty ( $session_data['new_price'] ) )
return $session_data;
// Get the new calculated price and update cart session item price
$session_data['data']->set_price( $session_data['new_price'] );
return $session_data;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and working. It will change all prices as you expect in cart, checkout and order pages…
I would like to ask how to add a custom fee to the woocommerce subscription recurring total?
Found this on the web:
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
However that certain function is just for the regular product. NOT SUBSCRIPTION — it doesn't add the fee to the recurring totals.
As of right now, WooCommerce subscriptions doesn't support adding fees to recurring totals.
This may be too late to be of any use to you, but you might find this useful: https://docs.woocommerce.com/document/subscriptions/develop/recurring-cart-fees/
I believe this example would be relevant to your question:
In some cases, you may require the fee to only apply to the on-going
recurring payment for any subscriptions. To achieve this, you need to ... add a condition to check whether
the cart being passed into your callback is a recurring cart, or the
standard WooCommerce cart.
Recurring carts have a $recurring_cart_key property we can use to
determine if the cart is a recurring cart.
add_filter( 'woocommerce_cart_calculate_fees', 'add_recurring_postage_fees', 10, 1 );
function add_recurring_postage_fees( $cart ) {
// Check to see if the recurring_cart_key is set
if ( ! empty( $cart->recurring_cart_key ) ) {
$cart->add_fee( 'Postage', 5 );
}
}
If someone comes by this question, you can solve it in the following way:
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );
add_filter( 'woocommerce_cart_calculate_fees', 'add_fees', 10 );
function add_fees($cart) {
$total_fee = $cart->get_subtotal();
WC()->cart->add_fee( 'Fee', $total_fee * 0.2 );
}
or
add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );
add_filter( 'woocommerce_cart_calculate_fees', 'add_fees', 10 );
function add_fees($cart) {
WC()->cart->add_fee( 'Fee', '10' );
}
I am fairly new to WooCommerce so I dont know what could be of use to answer my question, this is why I have not added any codeblocks.
I would like to let customers only add one product to the cart and if they add another product the current product in cart is replaced by the last one.
Do I need to make changes in the code or is it possible with a plugin or WooCommerce setting?
I am looking forward to helpful replies.
Thanks
/**
* When an item is added to the cart, remove other products
*/
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart.', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart', 10, 3 );
Add this code your theme functions.php file.
Hope It's useful !! Enjoy