So I have added a wordpress code snippet that shows for woocommerce products, the amount saved and % for the products on sale. However, adding that snippet also hides the variable product that are not on sale.
How can I make sure that the price of the variable product appears when it's not on sale.
If the condition holds true, it will return the original price without modification. This means that if a product isn't on sale or you're viewing it in the admin backend, the original price should still be shown.
However, I see potential problems that can cause the variable product's price to disappear when it's not on sale:
Your logic always assumes a product on sale will have both regular and sale prices. If, for some reason, the regular price is not set for a variation, your calculation and display logic will fail.
The way your HTML tags are structured might cause issues, especially the use of thetag at the beginning of the return.
Your logic always assumes a product on sale will have both regular and sale prices. If, for some reason, the regular price is not set for a variation, your calculation and display logic will fail.
The way your HTML tags are structured might cause issues, especially the use of thetag at the beginning of the return.
To ensure the price of the variable product appears when it's not on sale, you can make a couple of adjustments:
Refactor the price calculation and display logic to be more resilient and handle edge cases.
Clean up the HTML tags to ensure the correct price format.
Refactor the price calculation and display logic to be more resilient and handle edge cases.
Clean up the HTML tags to ensure the correct price format.
function imti_variation_price_format_310(price,product): string {
code
// Only on frontend
if (<REDACTED>()) {
return $price;
}
// If product is on sale
if ($product->is_on_sale()) {
$min_var_reg_price = $product->get_variation_regular_price('min', true);
$min_var_sale_price = $product->get_variation_sale_price('min', true);
// Check if regular and sale prices exist
if ($min_var_reg_price && $min_var_sale_price) {
$price_diff = round($min_var_reg_price - $min_var_sale_price);
$currency_symbol = <REDACTED>();
// Percentage calculation and text
$percentage = round(($min_var_reg_price - $min_var_sale_price) / $min_var_reg_price * 100) . '%';
$percentage_txt = ('<br/><p class="you-save" style="color:black;"> You Save: <mark style="background-color:rgba(0, 0, 0, 0); color:blue" class="vrpr">' . $currency_symbol . '' . $price_diff . '.00</mark> ('.$percentage. ') </p>');
return '<del>' . wc_price($min_var_reg_price) . '</del><ins>' . <REDACTED>) . '</ins>' . $percentage_txt;
}
}
// Return original price for non-sale products or if there are any issues with the sale price calculation
return $price;
}
This revised function will display the original price if the product isn't on sale or if there's any issue with the sale price calculation.