Tự động thêm NEW badge trong X ngày với Flatsome

Tự động thêm NEW badge trong X ngày với Flatsome 1

Flatsome có một chức năng là thêm badge cho sản phẩm như ảnh, các badge có thể là New, Sale…

Mặc định chúng ta có thể tự thêm vào trong admin, tuy nhiên với sản phẩm mới đăng chúng ta có thể thêm badge NEW tự động trong X ngày bằng cách thêm đoạn code sau vào functions.php của giao diện.

/**
 * Disable manual assigned NEW badge output.
 */
add_action( 'init', function () {
	remove_filter( 'flatsome_product_labels', 'flatsome_sale_flash', 10 );
} );

/**
 * 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 );

Thay đổi $days = 250; bằng số ngày muốn hiển thị của bạn

 

Chúc các bạn thành công

 

Nguồn

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

This site uses Akismet to reduce spam. Learn how your comment data is processed.