Reputation: 13
I am trying to display prices with and without VAT on the cart page. but when I call the price variable gives me zero value.
I used this snippet code:
function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) {
global $product;
$price = $product->price ;
$price_excl_tax = $price - round($price * ( 20 / 100 ), 2);
$price_excl_tax = number_format($price_excl_tax, 2, ",", ".");
$price = number_format($price, 2, ",", ".");
$display_price = ' <span class="amount" >';
$display_price .= '<span class=" eptax "><span class="nprice">£ '. $price_excl_tax.'</span><small class="woocommerce-price-suffix" style="color:black" > ex VAT</p></small> ';
$display_price .= '<span class=" ptax " ></small><span class="nprice">£ '. $price .'</span><small class="woocommerce-price-suffix" style="color:black"> inc VAT </p></small>';
$display_price .= '</span>';
return $display_price;
}
add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
And it gives me this:
Any advice?
Upvotes: 1
Views: 2525
Reputation: 29660
To display the price(s) on the cart page, in that specific column you can use the woocommerce_cart_item_price
filter hook.
Functions used:
wc_get_price_excluding_tax()
wc_get_price_including_tax()
So you get:
function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
// Get the product object
$product = $cart_item['data'];
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Price without VAT
$price_excl_tax = (float) wc_get_price_excluding_tax( $product );
// Price with VAT
$price_incl_tax = (float) wc_get_price_including_tax( $product );
// Edit price html
$price_html = wc_price( $price_excl_tax ) . '<span class="my-class"> ' . __( 'ex VAT', 'woocommerce' ) . '</span><br>';
$price_html .= wc_price( $price_incl_tax ) . '<span class="my-class"> ' . __( 'inc VAT', 'woocommerce' ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );
Upvotes: 3