WooCommerce – show the “NEW” badge for latest products

Content Views Pro helps you to select and show WooCommerce products easily without coding.
To display the NEW badge for WooCommerce products, Please add this code to file functions.php in the directory of your active theme:

// Content Views Pro - Display New badge for WooCommerce products
add_filter( 'pt_cv_field_thumbnail_image_html', 'cvp_theme_woocommerce_new_badge', 100 );
function cvp_theme_woocommerce_new_badge( $args ) {
	if ( PT_CV_Functions::get_global_variable( 'content_type' ) === 'product' ) {
		global $post;
		$hours	 = 72; /* TODO: change this value to number of hours to calculate */
		$diff	 = time() - mysql2date( 'U', $post->post_date );

		if ( $hours * 60 * 60 > $diff ) {
			$args = $args . "<span style='padding:2px 8px;color:#fff;background:red;position: absolute;left: 15px;top: 15px;'>New</span>";
		}
	}

	return $args;
}

Best regards,

Scroll to Top