1

The following code will display the discount saved under the price for both simple and variable products. But when I install the snippet code as suggested here "Replace the Variable Price range by the chosen variation price in WooCommerce 3", the discount saved for the variable products disappeared.

// For simple products
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
    global $product;

    if( $product->is_type('simple') && $product->is_on_sale() ) {
        $regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
        $active_price  = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );

        $saved_amount  = $regular_price - $active_price;
        $percentage    = round( $saved_amount / $regular_price * 100 );

        echo '<p id="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';
    }
}

// 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="saving_total_price">'. __("You Save") .': ' . wc_price($saved_amount) . ' ('.$percentage.'%)</p>';

    }
    return $data;
}
  • I just tried your code and it seems to work for both simple and variable products https://pasteboard.co/JaF2QCi.jpg – 7uc1f3r May 29 '20 at 20:04
  • What I am trying to achieve is to replace the variable prices and show the amount saved right below it. https://pasteboard.co/JaFEH9t.png – Kenneth Yap May 29 '20 at 21:37
  • @7uc1f3r https://pasteboard.co/JaFGwyf.png – Kenneth Yap May 29 '20 at 21:42
  • I am trying to make it work with this code https://stackoverflow.com/a/47476352/13643693 – Kenneth Yap May 29 '20 at 21:44
  • Well, for clarity, adjust your question accordingly. To me, it seemed like your current code was not working for variable products _... I am using the following code to display the amount saved on the single product page. The simple product shows the amount saved but it's not working with the variable products..._ – 7uc1f3r May 30 '20 at 05:46
  • 1
    I'm sorry about that, I have edited my post and hopefully it describes the issue clearer. Thanks. – Kenneth Yap May 30 '20 at 06:02

0 Answers0