WooCommerce Discount Alerts

Displays discount messages for simple and variable products on sale.

PHP
/**
 * Snippet Name:     WooCommerce Discount Alerts
 * Snippet Author:   coding-bunny.com
 * Description:      Displays discount messages for simple and variable products on sale.
 */

// Add discount alert for simple products
add_action('woocommerce_before_add_to_cart_form', 'cb_show_discount_alert_simple');

/**
 * Displays discount message for simple products if they are on sale.
 *
 * @return void
 */
function cb_show_discount_alert_simple() {
    global $product;

    // Check if the product is a valid WooCommerce product and is of type 'simple' and on sale
    if (is_a($product, 'WC_Product') && $product->is_type('simple') && $product->is_on_sale()) {
        $discounted_price = $product->get_sale_price();
        $regular_price = $product->get_regular_price();

        // Calculate discount and prepare the message if both prices are available
        if ($regular_price && $discounted_price) {
            $discount = $regular_price - $discounted_price;
            $discount_alert = '<div class="cb-sale-message">' . sprintf(__('You save %s', 'text-domain'), wc_price($discount)) . '</div>';
            $hurry_alert = '<div class="cb-sale-advise">' . __('Hurry up, if you don\'t want to miss this price!', 'text-domain') . '</div>';
            echo $discount_alert . $hurry_alert;
        }
    }
}

// Add discount alert for variable products
add_action('woocommerce_before_add_to_cart_form', 'cb_show_discount_alert_variable');

/**
 * Displays discount message for variable products if they are on sale.
 *
 * @return void
 */
function cb_show_discount_alert_variable() {
    global $product;

    // Check if the product is a valid WooCommerce product and is of type 'variable'
    if (is_a($product, 'WC_Product') && $product->is_type('variable')) {
        $variations = $product->get_available_variations(); // Get all available variations
        $max_discount = 0; // Initialize max discount variable

        // Loop through variations to find the maximum discount
        foreach ($variations as $variation) {
            $regular_price = (float) $variation['display_regular_price']; // Regular price of the variation
            $discounted_price = (float) $variation['display_price']; // Sale price of the variation

            // Calculate discount for this variation
            if ($regular_price && $discounted_price && $regular_price > $discounted_price) {
                $discount = $regular_price - $discounted_price;

                // Update max discount if the current discount is greater
                if ($discount > $max_discount) {
                    $max_discount = $discount;
                }
            }
        }

        // Prepare discount alert message if a discount was found
        if ($max_discount > 0) {
            $discount_alert = '<div class="cb-sale-message">' . sprintf(__('You can save up to %s', 'text-domain'), wc_price($max_discount)) . '</div>';
            $hurry_alert = '<div class="cb-sale-advise">' . __('Hurry up, if you don\'t want to miss this price!', 'text-domain') . '</div>';
            echo $discount_alert . $hurry_alert;
        }
    }
}

How To Implement This Solution?

Leave a Reply