Add Sales End Date for WooCommerce Product Price

There is always a situation when your customers will ask you whether the product sales price has a limited period. This always happen especially for a “Last Minute Sale”, “Christmas Eve Sale” or “Holiday Sale”.

In current WooCommerce product page, you mostly will see the strikethrough for normal price and beside will have the sales price. That’s it.

Normal Sales Price Layout for WooCommerce Product Page
Normal Sales Price Layout for WooCommerce Product Page

So in this tutorial, i will teach you all a simple way to add “Offer till [date end]” text beside the sales price. You can also change the text without any effort.

Let’s begin.

Step 1: Open functions.php file in your theme folder, add below code:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
	global $post;
	$sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
	if(is_single() && $sales_price_to != "")
	{
		$sales_price_date_to = date("j M y", $sales_price_to);

		return str_replace( '</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price );
	}
	else
	{
		return apply_filters( 'woocommerce_get_price', $price );
	}
}

Step 2: Save the file. If you want to change the text for above example, just change “Offer till” phrase to whatever you want, ok.


Step 3: Done, refresh your product page that has ongoing sales that you have schedule start and end date.

Add Price Sales End Date at WooCommerce Product Page
Add Price Sales End Date at WooCommerce Product Page

Thanks and i will always give you little tips to help you along the way, cheers!

35 thoughts on “Add Sales End Date for WooCommerce Product Price”

  1. Hi Terry,

    Thanks for the code. but unfortunately it’s not working for me. When put that code into function.php. my site is going down and my getting blank white pages.but i really want to show this expiration date just beside to my product price. Any help ?

    cheers

  2. Hi Terry,
    Can you talk about Countdown for a single product?
    Ex: I want Product name “ABC” with sale $19 & sale off $15 in 3 days (show countdown hours/minutes/seconds).

    Thanks,
    King

    1. Hi arianna, you can put extra spaces between price and the text. Do try the code below:

      add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
      function custom_price_html( $price, $product ){
          global $post;
          $sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
          if(is_single() && $sales_price_to != "")
          {
              $sales_price_date_to = date("j M y", $sales_price_to);
       
              return str_replace( '</ins>', ' </ins> &nbsp;&nbsp;&nbsp;<b>(Offer till '.$sales_price_date_to.')</b>', $price );
          }
          else
          {
              return apply_filters( 'woocommerce_get_price', $price );
          }
      }
      
  3. Terry, Hi!
    My question pertains to the schedule area of the:
    woo-product/product-data/general (back-end)
    Where we can schedule (via the calender) YY-MM-DD of the sale.
    Can I add HH:MM to that code to get hours and minutes?
    Sorry if this is a little off base, I’ve been searching everywhere for documentation and this is the closest I’ve found on the subject.
    Any ideas would be helpful, thank you again.

  4. hi Terry,
    I am using the Variable product type, and your code is not working with it.
    Any suggestion? That would be really helpful.

    1. Hi Lee, you can put below code at functions.php in your them folder:

      function wc_remove_related_products( $args ) {
      return array();
      }
      add_filter('woocommerce_related_products_args','wc_remove_related_products', 10);

  5. Thanks, but you misunderstood what I meant -.-

    I mean, your function (Add Sales End Date) will be displayed in the above related merchandise.

    Can be displayed only in one place ?

  6. It’s possible to add more than one schedule?
    I have to sell a course by October 20.
    Registering before August 31 pay 300.
    Registering before September 31 pay 400.
    Enrolling in October pay 500 (regular price)
    So I should add two dates sale price.
    Maybe I have to use some custom field?

  7. Hi sir,
    Please
    I added your code:
    add_filter( ‘woocommerce_get_price_html’, ‘custom_price_html’, 100, 2 );
    function custom_price_html( $price, $product ){
    global $post;
    $sales_price_to = get_post_meta($post->ID, ‘_sale_price_dates_to’, true);
    if(is_single() && $sales_price_to != “”)
    {
    $sales_price_date_to = date(“j M y”, $sales_price_to);

    return str_replace( ”, ‘    (Offer till ‘.$sales_price_date_to.’)‘, $price );
    }
    else
    {
    return apply_filters( ‘woocommerce_get_price’, $price );
    }
    }

    On my functions.php file, but it is not working !!!

    Please, Help!!!

    Best regards
    A.J.

  8. Hi sir,
    Your code is working very fins,
    Please,
    Do you know , how can i view a schedule time for a regular price ?

    Best regards
    A.J.

  9. Hello Terry,
    Thanks this code ! And how to display Sales End Date on the variable products page ? Can you help for me?
    Thanks

  10. Hey Terry,
    woocommerce_get_price_html is not being fired for products with variation. So your code only works for single products.
    Any idea how to fix that?
    Thank you!

  11. Hi, your code works like magic. However,

    1. kindly what the code will be if it were to show days to end of the sale instead? E.g Sale ends in 1day/2days/3days etc.

    2. I need it to display on the Sale Products page too.

    I will really appreciate a response, please.

    Thank you.

    Much love,
    from Africa.

  12. Is this still valid for WP 4.9 and WC 3.2.4 ?
    I am using a child theme and put the filter below the setup for the Child Theme Plug-in and it does not work. Any clue as to what I’m doing wrong?:

    ID, ‘_sale_price_dates_to’, true);
    if(is_single() && $sales_price_to != “”)
    {
    $sales_price_date_to = date(“j M y”, $sales_price_to);

    return str_replace( ”, ‘    (Offer till ‘.$sales_price_date_to.’)‘, $price );
    }
    else
    {
    return apply_filters( ‘woocommerce_get_price’, $price );
    }
    }
    // AMK – 171121 End
    //

Leave a Comment

Your email address will not be published. Required fields are marked *