I try to show the subtotal price and sale price in the Cart and Checkout page for each individual product. I already managed to add the sales price (24TL) to the regular price (33TL) with the following code:
/**
* Show sale prices on the Cart and Checkout pages
*/
function my_custom_show_sale_price( $old_display, $cart_item, $cart_item_key ) {
$price_string = $old_display;
$product = $cart_item['data'];
if ( $product ) {
$price_string = $product->get_price_html();
}
return $price_string;
}
add_filter( 'woocommerce_cart_item_price', 'my_custom_show_sale_price', 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'my_custom_show_sale_price', 10, 3 );
Unfortunately the above code shows the product price only for one piece not for the subtotal (in this case it should be 66TL / 48TL) in the cart or checkout page.
After some digging i found the following price filter to show the price per item as subtotal in my default cart.php file - where the price displayed the correct way before i added the sales price to it with the code form above.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
}
}
Any idea how to change the first block of code to show the product price per item as subtotal?
Updated: You can use the following to display cart item on sale prices formatted range:
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( $cart_item['data']->is_on_sale() ) {
return $cart_item['data']->get_price_html();
}
return $price_html;
}
add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal_html, $cart_item, $cart_item_key ){
$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
$tax_string = '';
if ( $product->is_taxable() ) {
if ( WC()->cart->display_prices_including_tax() ) {
$regular_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
$active_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
if ( ! wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
$tax_string = ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
}
} else {
$regular_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity, 'price' => $product->get_regular_price() ) );
$row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
if ( wc_prices_include_tax() && WC()->cart->get_subtotal_tax() > 0 ) {
$tax_string = ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
} else {
$regular_price = $product->get_regular_price() * $quantity;
$active_price = $product->get_price() * $quantity;
}
if( $product->is_on_sale() ) {
return wc_format_sale_price( $regular_price, $active_price ) . $product->get_price_suffix() . $tax_string;
}
return $subtotal_html;
}
Code goes in functions.php file of the active child theme (or active theme). tested and works.