Hi there,
I tried to add these codes to the child theme functions.php, but for some reason these didn’t work:
function you_save_text_for_product() {
global $product;
// works for Simple and Variable type
$regular_price = get_post_meta( $product->get_id(), '_regular_price', true ); // 36.32
$sale_price = get_post_meta( $product->get_id(), '_sale_price', true ); // 24.99
if( !empty($sale_price) ) {
$saved_amount = $regular_price - $sale_price;
$currency_symbol = get_woocommerce_currency_symbol();
$percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
?>
<p class="you_save_price">You save: <?php echo $currency_symbol .''. number_format($saved_amount, 2, '.', ''); ?></p>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'you_save_text_for_product', 12 ); // hook priority
I tried to add this for the variable products:
// For product variations (on variable products)
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$saved_amount = $data['display_regular_price'] - $data['display_price'];
$percentage = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= '<p id="you_save_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
}
return $data;
}
This to CSS:
.you_save_price {
background: #f9f9f9;
padding: 10px;
border: 1px dashed #ddd;
width: 150px;
font-size: 14px;
font-weight: 600;
margin-top: 10px;
}
How can I make this work properly? Is this part wrong on the code: woocommerce_single_product_summary or the 12 ); // hook priority ?
I took the codes from here:
https://wpsimplehacks.com/simple-woocommerce-hacks/#hack-6-how-to-display-you-save-for-sale-price
https://www.youtube.com/watch?v=auGuzky6QWI
Best Regards,
Tuomo Nurkkala