Display sale price on WooCommerce single product pages

I’m looking for a solution to calculate and show the customer’s profit for all WooCommerce products (simple and variable).

The current code works for simple products, but not for variable products. Additionally, it causes the WooCommerce payment page to crash.

What is the best code or solution for this?

Solution

Currently, the code below works for simple products to calculate and show the customer’s profit, but not for variable products and causes the WooCommerce payment page to crash.

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;
        echo '<p id=&quotsaving_total_price&quot>'. __(&quot your profit &quot) .': ' . wc_price($saved_amount) . ' </p>';}
}

Is there a better code or solution that can calculate and show the customer’s profit for all WooCommerce products (simple and variable) without crashing the WooCommerce payment page?

To calculate and show the customer’s profit for all WooCommerce products (simple and variable) without crashing the WooCommerce payment page, you can use the following code:

add_action( 'woocommerce_single_product_summary', 'product_profit_amount', 11 );
function product_profit_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;
        echo '<p id="profit_total_price">' . __( 'Your profit: ', 'text-domain' ) . wc_price( $saved_amount ) . '</p>';
    } elseif ( $product->is_type( 'variable' ) && $product->is_on_sale() ) {
        $saved_amount = 0;

        foreach ( $product->get_children() as $variation_id ) {
            $variation = wc_get_product( $variation_id );
            $regular_price = (float) wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) );
            $active_price  = (float) wc_get_price_to_display( $variation, array( 'price' => $variation->get_sale_price() ) );
            $saved_amount += $regular_price - $active_price;
        }

        echo '<p id="profit_total_price">' . __( 'Your profit: ', 'text-domain' ) . wc_price( $saved_amount ) . '</p>';
    }
}

This updated code adds support for variable products by calculating the profit for each variation and summing them up. It checks if the product is a variable product using $product->is_type( 'variable' ) and iterates over each variation using $product->get_children(). The profit amount is then displayed using the wc_price() function.

Make sure to replace 'text-domain' with your actual text domain in the code.

Please note that if you’re experiencing performance issues or crashes on the WooCommerce payment page, it’s recommended to optimize your code and consider caching mechanisms to improve performance.