Hi there,
The custom code works on my test website but doesn’t work on some products also.
That is because the code will not work on variable products. When you choose a variation, it doesn’t get processed by PHP but by Javascript.
You would have to create an AJAX custom code to handle variable products.
Let us know how we could help you further.
Kind Regards,
Michael
but that code works on some variable products, it works in shirts with attibutes of 2 colors and sizes like this one: https://www.runayaq.com/producto/runayaq-mujer-polo-huaylarsh/
but not in a product with attributes of just 1 color and sizes.
https://www.runayaq.com/producto/runayaq-mujer-polo-valle-del-mantaro/
Hi @freddyeee,
Based on the information you provided, it seems like the issue might be related to the product’s attributes. In some cases, when a product has attributes, the code used to display the “You save(x%)” message might not work properly.
One possible solution could be to modify the code to handle the product’s attributes more gracefully. For example, you might need to adjust the code to check for the presence of attributes and handle them differently depending on the type of attribute (e.g., colour, size, etc.).
Here is an updated code snippet that should handle both simple and variable products with attributes:
add_action( 'woocommerce_single_product_summary', 'display_savings_percentage', 12 );
function display_savings_percentage() {
global $product;
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
if ( ! $regular_price || ! $sale_price ) {
return;
}
if ( $product->is_type( 'variable' ) ) {
$min_price = $product->get_variation_regular_price( 'min', true ); $max_price = $product->get_variation_regular_price( 'max', true ); $min_sale_price = $product->get_variation_sale_price( 'min', true ); $max_sale_price = $product->get_variation_sale_price( 'max', true );
if ( ! $min_price || ! $max_price || ! $min_sale_price || ! $max_sale_price ) {
return;
}
$regular_price = ( $min_price === $max_price ) ? $min_price : $min_price . ' - ' . $max_price;
$sale_price = ( $min_sale_price === $max_sale_price ) ? $min_sale_price : $min_sale_price . ' - ' . $max_sale_price;
}
$savings_percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 );
echo '<p class="savings-percentage">You save ' . $savings_percentage . '%</p>'; }
This code should check if the product is a variable product, and if so, calculate the minimum and maximum regular and sale prices for all variations, and then display the appropriate message with the savings percentage.
If the product is a simple product, the code should work as before and calculate the savings percentage based on the regular and sale prices.
If the above solution doesn’t work, I will advise that you consider reaching out to WooCommerce for further assistance.
I hope this helps.
Cheers,
Eze
Now the message doesn’t show in any product page with this last code.
Hi @freddyeee,
Have you tried reaching out to WooCommerce regarding this?
Kindly,
Hannah