The post titled "How to set and display the WooCommerce product expiration date and time in WordPress" explains how to set and display the product expiration date in WooCommerce. In this post, I will discuss how to display the time left until the product end date.
It's possible to display time remaining by comparing the post expiration date (set by Post Expirator) and the current date. We need to modify a file of the Post Expirator plugin: post-expirator.php (located under the Post Expirator plugin folder.)
- Open the "post-expirator.php " file within the Post Expirator plugin folder
- Go to the code "function postexpirator_shortcode($atts)"
- Copy the "function postexpirator_shortcode($atts)" code snippet and paste it immediately under this function
- Change the function name and shortcode (For example, I changed "postexpirator" to "postexpiratordiffer". You can change it as you wish.)
- Change "return get_date_from_gmt(gmdate('Y-m-d H:i:s',$expirationdatets),$format);" to "return gmdate('Y-m-d H:i:s',$expirationdatets);" Or you can change it as shown:
Now, we need to add a code to compare the expiration date and the current dat and then display the time left to the WooCommerce template file: short-description.php (located under "/woocommerce/wp-content/plugins/woocommerce/templates/single-product/" folder.)
You can use the following code for the WooCommerce product expiration date and the current date:
$expriationdate = do_shortcode( '[postexpiratordiffer]' ); $now = date('Y-m-d H:i:s');
You can use the code found on this page for date difference.
$expriationdate = do_shortcode( '[postexpiratordiffer]' ); $now = date('Y-m-d H:i:s'); $diff = abs(strtotime($now) - strtotime($expriationdate)); $years = floor($diff / (365*60*60*24)); ...
Please replace "..." with the code snippet of the function "diff()" found on the page "Date difference between two timezones in PHP."
An exmaple showing time remaining until the product end date:
Reference
The final code will be similar to:
<?php $tempCheck = do_shortcode( '[postexpirator]' ); if(empty($tempCheck)) { echo '<div class="expriacy-date">Expired on: None</div>'; } else { echo '<div class="expiracy-date">Expired on ' . do_shortcode( '[postexpirator]' ) . '</div>'; $expriationdate = do_shortcode( '[postexpiratordiffer]' ); $now = date('Y-m-d H:i:s'); echo '<div id="autoexpirationdate" style="font-size: 12px; color: red">('; $diff = abs(strtotime($now) - strtotime($expriationdate)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); $days2 = floor(($diff - $years * 365*60*60*24)/ (60*60*24)); $hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60)); $minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ (60)); $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); printf("%d Dates %d Hours %d Minutes %d Seconds\n", $days2, $hours, $minutes, $seconds); echo ' left)</div>'; } ?>
Instead of editing the WooCommerce files, you can use a filter:
// Displaying the expiration date for WooCommerce Products function woocommerce_expiration_date ( $desc ){ global $product; if ( is_single( $product->id ) ){ $tempCheck = do_shortcode( '[postexpirator]' ); if(empty($tempCheck)) { $desc .= '<div class="expriacy-date">Expired: None</div>'; } else { $desc .= '<div class="expiracy-date">Expired: ' . do_shortcode( '[postexpirator]' ) . '</div>'; $expriationdate = do_shortcode( '[postexpiratordiffer]' ); $now = date('Y-m-d H:i:s'); $desc .= '<div id="autoexpirationdate" style="font-size: 12px; color: red">('; $diff = abs(strtotime($now) - strtotime($expriationdate)); // same with the above code (from $years through $minutes lines) $seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60)); $desc .= $days2 . 'days ' . $hours . 'hours ' . $minutes . 'minutes ' . $seconds . 'seconds'; $desc .= ' left)</div><br>'; } return $desc; } } add_filter( 'woocommerce_short_description', 'woocommerce_expiration_date' );
Hello there,
I have done everything you write but i got wrong countdown. E.g the product expires in 9 hours
but instead of : a "0 days 9 hours 0 minutes 0 seconds"
i see ": 0 days 0 hours 0 minutes 3 seconds
"
What i have to check?
Thank you
Hello, wiseprt.
Thanks for your comment. I think it's related to timezone. I stays in Seoul whose timezone is GMT + 9. So the code might have worked well here but you might have 9 hours difference...
Could you please try to change gmdate to date or something? Or if it does not work, could you please try to offset the timezone difference: 9 hours (for example, by utilizing the code at http://php.net/manual/en/function.timezone-offset-get.php)?
Appreciated if you let me know the corrected code for this issue.
Please feel free to contact me anytime if you have any questions.
Or, you might use the following code instead of "$now = date('Y-m-d H:i:s');"
$now = date('Y-m-d H:i:s', strtotime('+9 hours'));
OR
$now = date('Y-m-d H:i:s', time()+32400); // 3600 sec in hour
Please refer to http://stackoverflow.com/questions/7110496/add-a-1-hour-expiration-to-the-current-time-date