Display discount on shop page woocommerc

// Display discount on shop page
add_action( 'woocommerce_after_shop_loop_item_title', 'display_discount_on_shop_page', 15 );

function display_discount_on_shop_page() {
global $product;

// Check if the product is on sale
if ( $product->is_on_sale() && ($product->is_type('simple')) ) {
// Get regular price and sale price
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();

// Calculate discount percentage
$discount_percentage = round( ( ($regular_price - $sale_price) / $regular_price ) * 100 );

// Display discount percentage
echo '<span class="discount-badge">' . $discount_percentage . '% Off</span>';
}
}

Leave a comment