Skip to content

Instantly share code, notes, and snippets.

@juniorthiesen
Created September 12, 2020 16:02
Show Gist options
  • Save juniorthiesen/6663935fefc8ea8fb905010143ce1905 to your computer and use it in GitHub Desktop.
Save juniorthiesen/6663935fefc8ea8fb905010143ce1905 to your computer and use it in GitHub Desktop.
Add automatic NEW badge
/**
* Code goes in theme functions.php
*
* Add automatic NEW badge for x amount of days.
*
* Keep caching in mind! If you want to automatically update it on the frontend by using caching, you might want to look
* into a cron job that clears cache each day f.ex. https://docs.wp-rocket.me/article/494-how-to-clear-cache-via-cron-job
*/
add_filter( 'flatsome_product_labels', function ( $text, $post, $product, $badge_style ) {
$datetime_created = $product->get_date_created();
$timestamp_created = $datetime_created->getTimestamp();
$datetime_now = new WC_DateTime();
$timestamp_now = $datetime_now->getTimestamp();
$time_delta = $timestamp_now - $timestamp_created;
$days = 250; // Change amount of days here.
$days_in_seconds = 60 * 24 * 60 * $days;
if ( $time_delta < $days_in_seconds ) {
$text .= '<div class="badge callout badge-' . $badge_style . '"><div class="badge-inner callout-new-bg is-small new-bubble">NEW!</div></div>';
}
return $text;
}, 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment