Support » Plugin: WooCommerce » Get WooCommerce Scheduled Sale End Date

  • Resolved mattmikulla


    Does anyone know how to get a product’s scheduled sale end date and output it in a template?

    It seems like this would be an excellent feature built into themes to introduce something like a sale countdown timer. Countdown timers can definitely increase sales.

    I’d be fine with just knowing how to output the date for now.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Both values are stored in a postmeta field. You can query those by using the get_post_meta function, like our plugin does:

    $sale_price_dates_from 	= ( $date = get_post_meta( $thepostid, '_sale_price_dates_from', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';
    $sale_price_dates_to 	= ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';

    The variable $thepostid here is the id of the product you are trying to get the from sales date and to sales date from. The $sale_price_dates_from and $sale_price_dates_to variables will contain dates you want to have.

    Thanks Coen.

    Is it best to plug the above code into a functions.php file and hook into something or output by placing code in a template?

    This really depends on where you want it to show. You can use this code wherever you want, you just have to echo the variables $sale_price_dates_from and $sale_price_dates_to where you want them to be shown.

    Is it possible to get this to work for variable products?

    You can’t set a scheduled sale end date for all variations of product at once like you can quantity.

    Most of my products are variable products.

    This worked for simple products but not variable products:

    add_action( 'woocommerce_single_product_summary', 'woocommerce_output_sale_end_date', 10 );
    function woocommerce_output_sale_end_date() {
    $sale_end_date = get_post_meta( get_the_ID(), '_sale_price_dates_to', true );
    if ( ! empty( $sale_end_date ) )
    echo '<span id="sale-price-to-date">' . __( 'Sale End Date: ', 'woocommerce' ) . date( 'Y-m-d', $sale_end_date ) . '</span>';

    I have wrote a post on this topic before, you can have a look at the link below:

    Add Sales End Date for WooCommerce Product Price

    Thanks @terrytsang.

    I can’t test this right now but will your solution work for variable products?

    If not I can’t use it.

    The code I gave you earlier will also work for variable products. You just need to provide the id of the variation, instead of the main product id.

    Bulk edit support for scheduled sales price start/end date is indeed not in the WooCommerce plugin right now. Please use our ideas board to vote for this new feature, if you like to.

    I’m at a loss here.

    My understanding of PHP is so so. I can learn from example though.

    You just need to provide the id of the variation, instead of the main product id.

    Would I use? get_variation_id

    If so I modified the following function and replaced get_the_ID with get_variation_id and am getting an undefined function error:

    add_action( 'woocommerce_single_product_summary', 'woocommerce_output_sale_end_date', 10 );
    function woocommerce_output_sale_end_date() {
    $sale_end_date = get_post_meta( get_variation_id(), '_sale_price_dates_to', true );
    if ( ! empty( $sale_end_date ) )
    echo '<span id="sale-price-to-date">' . __( 'Sale End Date: ', 'woocommerce' ) . date( 'Y-m-d', $sale_end_date ) . '</span>';

    Any idea why that wouldn’t work?

    The function get_variation_id() is actually a method of the WC_Product_Variation class. In order to use it, you will need a WC_Product_Variation instance object. But if you still have to make that, chances are that you will also have the id already.

    From what variation are you trying to get the sale end date from? Is it a variation that is selected in the form on a single product page?

    Yes. The variation is selected on the single product page.

    I’m not a developer but I can learn from example and implement what needs to be done.

    Can you please provide a working code example with the WC_Product_Variation instance you describe?

    Right, well it is a bit different if you want the sale date to be shown based on the current selection. You need a way to determine what selection is made and find the scheduled sales date based on that selection.

    The first part is done like this:

    add_action( 'woocommerce_after_add_to_cart_button', 'woocommerce_output_sale_end_date', 10 );
    function woocommerce_output_sale_end_date() {
    	<script type="text/javascript">
    	jQuery(function( $ ) {
    		$('input[name=variation_id]').on( 'change', function(event) {
    			if ( $(this).val() != '' ) {
           			// DO AJAX CALL HERE BASED ON: $(this).val()

    At the line of the // DO AJAX CALL HERE you’ll need to do an Ajax call (read more about that here: ) to get the scheduled sales dates for the value we check for there. This value will be a variation id and can be used to get a WC_Product_Variation object using our get_product() function.

    So in the Ajax call, you’ll do this (part of the original code). You will not need to make a full instance in this case, since you already have the id:

    $variation_id = $VALUE_POSTED_BY_AJAX;
    $sale_end_date = get_post_meta( $variation_id, '_sale_price_dates_to', true );
    return '<span id="sale-price-to-date">' . __( 'Sale End Date: ', 'woocommerce' ) . date( 'Y-m-d', $sale_end_date ) . '</span>';

    And once that is returned, you’ll need to output the data returned.

    This is as far as I can go without writing a full custom plugin for you. If you need more help beyond this, I advise you to find someone who can do this as a paid job:

    Affiliated Woo Workers:

    Thank you so much Coen. I will roll with what you provided and try to figure it out.

    Can someone help me add HH:MM to the WooCommerce Scheduled Sale Date.
    As of now it’s YY-MM-DD we like to get it to YY-MM-DD HH:MM

    Any ideas would be helpful,
    Thank you

    	   $sale_price_dates_from  = ( $date = get_post_meta( $thepostid, '_sale_price_dates_from', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';
           echo  ($sale_price_dates_from);
    	  $sale_price_dates_to 	= ( $date = get_post_meta( $thepostid, '_sale_price_dates_to', true ) ) ? date_i18n( 'Y-m-d', $date ) : '';
    	  echo ($sale_price_dates_to);

    this is my code for dates, i like to show Sale Price Dates in template but it is not working, do i miss something?

    Any ideas are welcomed

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Get WooCommerce Scheduled Sale End Date’ is closed to new replies.