Untitled
unknown
plain_text
a year ago
918 B
8
Indexable
add_filter( 'woocommerce_cart_item_price', 'custom_cart_item_price_display', 99999999, 3 );
function custom_cart_item_price_display( $price, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
if ( $product->is_on_sale() ) {
// Get regular and sale prices
$regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
$sale_price = wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
// Calculate discount percentage
$discount_percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
// Format the price display
$price = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . '</ins>';
$price .= ' <span class="discount-percentage">(' . $discount_percentage . '% off)</span>';
}
return $price;
}Editor is loading...
Leave a Comment