Browse: Home / Snippets /

Display Regular Price & Sale Price on Course using custom fields

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

This allows you to display both regular and sale prices on LearnDash course pages. If a course has both prices set in the custom fields (regular_price and sale_price), it displays the regular price crossed out with the sale price highlighted. If only a regular price is set, it displays the regular price alone.

Just a Note: This snippet modifies the display of the price on the course page, but users must still input the actual sale price in the “Buy Now” price field in the LearnDash course settings to ensure the correct price is charged during checkout.


add_filter('learndash_get_course_price', 'custom_display_regular_and_sale_price', 10, 1);

function custom_display_regular_and_sale_price($pricing) {
    // Get the current course ID
    $course_id = get_the_ID();

    // Retrieve regular and sale prices from custom fields
    $regular_price = get_post_meta($course_id, 'regular_price', true);
    $sale_price = get_post_meta($course_id, 'sale_price', true);

    // If both regular and sale prices are set, modify the display price
    if (!empty($regular_price) && !empty($sale_price)) {
        $pricing['price'] = '<span class="regular-price" style="text-decoration: line-through; color: grey;">' . esc_html($regular_price) . '</span> ';
        $pricing['price'] .= '<span class="sale-price" style="color: red; font-weight: bold;">' . esc_html($sale_price) . '</span>';
    } elseif (!empty($regular_price)) {
        // If only the regular price is set, display it alone
        $pricing['price'] = '<span class="regular-price">' . esc_html($regular_price) . '</span>';
    }

    return $pricing;
}