Display "excl. VAT" in Woocommerce product-subtotal - php

in Woocommerce when you choose to display prices during checkout inclusive VAT, Woocommerce then adds a .tax_label after the td.product-subtotal, and a .includes_tax after the total. For customers from countries with 0% VAT, nothing is displayed, I need the tax_label and includes_tax to be displayed though as “excl. VAT”. So in the td.product-subtotal cell there should be the subtotal price with the added "excl. VAT" and in the order-total.td there should be the total price also with the added "excl.VAT".
add_filter( 'woocommerce_cart_hide_zero_taxes', '__return_false' );
Above filter does not do what I want because it does not add "excl.VAT" to the td.product-subtotal.

If you're talking about the cart subtotal there is a filter for that woocommerce_cart_subtotal.
<?php
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) {
if( ! empty( $cart_subtotal ) ) {
return $cart_subtotal . ' - excl. VAT';
};
}; ?>
Untested but should be working.

Related

Woocommerce How to Display Sale Price of Products in Cart

I have set up my cart to display the product price with a strikethrough next to the sale price of the discounted item (see first product in cart in photo).
checkout screenshot
This was achieved using this code
function my_custom_show_sale_price_at_cart( $old_display, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
if ( $product ) {
return $product->get_price_html();
}
return $old_display;
}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price_at_cart', 10, 3 );
The issue as you can see in the photo is that this only works for products on sale (the $12.00 product on sale for 0.00). However, a coupon code is applied to the other two items.
I followed this thread to display the total savings as "You Saved" in the checkout summary, including sale and coupon discounts.
How can I display the discounted price of the items in the cart that have the coupon applied to them?
So this will update the cart item totals and the line item totals based on what I interpreted your comments to be.
This would be added to functions.php
function my_custom_show_sale_price_at_cart( $old_display, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
// If you just want this to show in checkout vs cart + checkout - add && is_checkout() below.
if ( $product ) {
// This item has a coupon applied.
if ( floatval( $product->get_price() ) !== number_format( $cart_item['line_total'], 2 ) / $cart_item['quantity'] ) {
// This updates the item total.
$price_html = wc_format_sale_price( $product->get_regular_price(), number_format( $cart_item['line_total'], 2 ) ) . $product->get_price_suffix();
} else {
$price_html = $product->get_price_html();
}
// This updates the line item sub-total.
add_filter(
'woocommerce_cart_item_subtotal',
function() use ( $cart_item ) {
return wc_price( $cart_item['line_total'] + $cart_item['line_tax'] );
}
);
return $price_html;
}
return $old_display;
}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price_at_cart', 10, 3 );

WooCommerce doesn't show actual price on cart page without tax

I have build webstore on WooCommerce. I have added standard tax rate when the customer fills their billing address and select country tax will be applied on the checkout page that is working fine I want to show actual price of products on cart page without multiply my tax ratio with my product price
This is checkout page show tax when a country is selected
this is cart page showing tax calculated value and grand total show without tax value
I have added this function in my function.php file but no success
function wc_remove_cart_tax_totals( $tax_totals, $instance ) {
if( is_cart() ) {
$tax_totals = array();
}
return $tax_totals;
}
add_filter( 'woocommerce_cart_tax_totals', 'wc_remove_cart_tax_totals', 10, 2 );
// Show the cart total excluding tax.
function wc_exclude_tax_cart_total( $total, $instance ) {
// If it is the cart subtract the tax
if( is_cart() ) {
$total = round( WC()->cart->cart_contents_total + WC()->cart->shipping_total + WC()->cart->fee_total, WC()->cart->dp );
}
return $total;
}
add_filter( 'woocommerce_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );
add_filter( 'woocommerce_subscriptions_calculated_total', 'wc_exclude_tax_cart_total', 10, 2 );

WooCommerce Filter woocommerce_adjust_non_base_location_prices in cart

I try to show prices based on the customers country (price incl. country based tax). This works great. The customer pays always € 119.00, no matter where he lives. Only the tax is adjusting.
This works only with the following code:
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
Now I have the following challenge: There are countries with 0% tax, because they are paying the tax in their own country (like Norway). If the tax rate is 0% I want to charge the price without tax (standard rate of our shop is 19%) - in the case of Norway it is just € 100.00.
So I tried the following code:
function disable_woo_vat_adjustment(){
$total_tax = WC()->cart->get_subtotal_tax();
if( isset( $total_tax ) && $total_tax > 0 ){
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
}
}
do_action( 'after_setup_theme', 'disable_woo_vat_adjustment' );
This code still works for Norway, the price is € 100.00 and this is correct. But now the price for all other countries is wrong, because the price is not always the same (€ 119.00), instead it is € 100.00 + tax (for Austria 20.00% => € 120.00 instead of € 119.00).
I tried to add a second check in the if clause:
function disable_woo_vat_adjustment(){
$total_tax = WC()->cart->get_subtotal_tax();
if( isset( $total_tax ) && !empty( $total_tax ) && $total_tax > 0 ){
add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
}else{
remove_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
}
}
do_action( 'after_setup_theme', 'disable_woo_vat_adjustment' );
I've also tried not to remove the function, but to set the filter to __return_true in the else condition, but didn't work as well for all other countries (like Austria).
What am I missing?
Thank you.

Custom cart item price based on user input in Woocommerce

in our Woocommerce store we have some minimum price for any products . And in every product inner page there is two filed where customer can type width , height of the product . And then they can add this product to cart, then price is changed based on given width and height.
For example if the minimum price for a product is 50 . And customer add width =2, height=3 , then the price for this product is going to 50*2*3=300
So to arrange this we add this code to function.php
add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {
$result=$_POST['width']*$_POST['height']*$cart_item_data['data']->price;
$cart_item_data['new-price'] =$result;
$cart_item_data['data']->set_price($result);
return $cart_item_data;
}
So this is working correctly .
But the problem is once it is coming to checkout page then the product price is showing as 50 , but it is 300 actually.
so to overcome this i use the following code
add_filter( 'woocommerce_cart_item_subtotal', 'update_with_custom_details', 10, 3 );
function update_with_custom_details( $subtotal, $cart_item, $cart_item_key ) {
$subtotal =" £".$cart_item['line_total'];
return $subtotal;
}
now it is showing £300, but i know this code is wrong.
The code is wrong because when customer apply a 50% coupon code for this product then the discount is coming as 25 , Because it calculating based on 50*.5=25; But actually product new price is 300, so the discount should be 300*5=150;
So how i can update the product price in cart page, checkout page ,mini cart etc ?
Please help .
Please see this also
Woocommerce product custom price is not accepted by wocommerce coupon
Update 2: The correct way to do it need to be done in two steps:
We calculate the price on add to cart event hook and save it as custom cart item data.
We set that calculated price in woocommerce_before_calculate_totals hook
The code:
// Save custom field value in cart item as custom data
add_filter( 'woocommerce_add_cart_item_data', 'calculate_custom_cart_item_prices', 30, 3 );
function calculate_custom_cart_item_prices( $cart_item_data, $product_id, $variation_id ) {
if ( isset($_POST['width']) && isset($_POST['height']) ) {
// Get the correct Id to be used (compatible with product variations)
$the_id = $variation_id > 0 ? $variation_id : $product_id;
$product = wc_get_product( $the_id ); // Get the WC_Product object
$product_price = (float) $product->get_price(); // Get the product price
// Get the posted data
$width = (float) sanitize_text_field( $_POST['width'] );
$height = (float) sanitize_text_field( $_POST['height'] );
$new_price = $width * $height * $product_price; // Calculated price
$cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
}
return $cart_item_data;
}
// Set custom calculated price in cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 );
function set_calculated_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( ! empty( $cart_item['calculated-price'] ) ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( $cart_item['calculated-price'] );
}
}
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
For testing purpose, I have used the following code that output 2 custom fields on single product pages:
// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
global $product;
// The fake calculated price
?>
<div>
<label class="product-custom-text-label" for="servicetime"><?php _e( 'Custom text:', 'woocommerce'); ?><br>
<input type="text" id="user-width" name="width" value=""><br>
<input type="text" id="user-height" name="height" value="">
</label>
</div><br>
<?php
}

Checkout price issue using woocommerce_product_get_price hook

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…

Categories