Skip Navigation

[Resolved] Customize the sale badge with sale percentage

This support ticket is created 7 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by matteoF 7 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#407585

Hi,
I need to insert a custom text containing the sale percentage on product, such as "save 12%".
it's possible ? maybe by adding extra option in [WPV-woo-onsale] shortcode...

#407677

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matteo,

Thank you for contacting our support forum.

Let me see how best I can assist you with this 🙂

Doing some reading I was able to stumble on a snippet of code that should be able to assist you with this.

Add the following to your functions.php file and it should allow you to customize your sale text.

<?php
// Add save percent next to sale item prices.
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
	$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
	return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
?>

Please let me know if this helps.
Thanks,
Shane

#407855

your filter adds 'Save %' to the price (return $price....)
I need to create a shortcode to use individually like [wpv-woo-onsale]. It's possible do it without edit sale-flash.php to avoid changes in future with the woocommerce updates.

#408006

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matteo,

What you can do is to wrap the function in a shortcode like this.


// Add Shortcode
function get_sale_price( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'product_id' => '',
		),
		$atts
	);
	if( function_exists('get_product') ){
		
		$sale_price = get_post_meta ( $atts['product_id'], '_sale_price');
		return 'sale price '.$sale_price[0];
	}

}
add_shortcode( 'get_sale_price', 'get_sale_price' );

You can use the shortcode by doing the following.

[get_sale_price product_id='[wpv-post-id]']

Please let me know if this helps.

Thanks,
Shane

#408050

I need to display the percentage on discount and not the sale price.

Editing your code I built this one but without results, all value are 0:

// Add Shortcode
function get_sale_price( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'product_id' => '',
        ),
        $atts
    );
    if( function_exists('get_product') ){ 
        $sale_price = get_post_meta ( $atts['product_id'], '_sale_price', true);
        $regular_price = get_post_meta ( $atts['product_id'], '_sale_price', true);
        $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
        return 'Save ' . $percentage.'%';
    }
 
}
add_shortcode( 'get_sale_price', 'get_sale_price' );

So I built this code and it works even if slowly:

// Add Shortcode
function get_sale_price( $atts ) {
 
    if( function_exists('get_product') ){
        $sale_price = get_post_meta( get_the_ID(), '_sale_price', true);
        $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
        $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
        return 'Save ' . $percentage.'%';
    }
 
}
add_shortcode( 'get_sale_price', 'get_sale_price' );

I resolved in this way
Thank you for your assistance

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.