Preview:
/**
 *  Product badges
 */

// Discount in percentage % [discount_percentage]
add_shortcode('discount_percentage', function() {
    global $product;

    if ( ! $product || ! $product->is_on_sale() ) {
        return '';
    }

    $regular_price = floatval( $product->get_regular_price() );
    $sale_price = floatval( $product->get_sale_price() );

    if ( $regular_price <= 0 || $sale_price >= $regular_price ) {
        return '';
    }

    $discount = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
    return '-' . $discount . '%';
});
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter