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...
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
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.
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
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