I do not work with the Bricks Products element and would like to use the label. For the “Sale” label this is possible with {woo_product_on_sale}. How can I use the “New” label?
To use the “New” label in Bricks without the Bricks Products element, you need to add a custom field or tag to mark products as new. Unfortunately, there isn’t a direct shortcode like {woo_product_on_sale} for the “New” label, so you’ll need to implement custom code or use a plugin that supports this feature.
Thank you. I thought that you might be able to access the Bricks function directly. Then you could write your own script and specify the duration in the Bricks settings. Do you understand what I mean?
I know that there is no shortcode.
But there is the Bricks function. This function is already written and I wonder if you can access this function to use the “New” element in your own script or even generate a custom dynamic tag.
Creating a custom field is not an option. It must work automatically. Do this for hundreds of products every month.
function is_product_new($product_id = null) {
// If no specific product ID is provided, get the current product ID
if (!$product_id) {
$product_id = get_the_ID();
}
// Get the product's publish date
$publish_date = get_the_date('Y-m-d', $product_id);
// Convert publish date to timestamp
$publish_timestamp = strtotime($publish_date);
// Get the current timestamp
$current_timestamp = current_time('timestamp');
// Calculate the difference in days between the current date and publish date
$date_diff = ($current_timestamp - $publish_timestamp) / (60 * 60 * 24);
// Return 1 if the product is less than or equal to 30 days old, otherwise return 0
return $date_diff <= 30 ? 1 : 0;
}
Edit this to line to set how many days old the product should be:
// Return 1 if the product is less than or equal to 30 days old, otherwise return 0
return $date_diff <= 30 ? 1 : 0;
Don’t forget to register the function to use in bricks dynamic data.
Then use this to show your badge:
If you would like to set the number of days in the Bricks Conditions use this instead:
function is_product_new($days = 30, $product_id = null) {
// If no specific product ID is provided, get the current product ID
if (!$product_id) {
$product_id = get_the_ID();
}
// Get the product's publish date
$publish_date = get_the_date('Y-m-d', $product_id);
// Convert publish date to timestamp
$publish_timestamp = strtotime($publish_date);
// Get the current timestamp
$current_timestamp = current_time('timestamp');
// Calculate the difference in days between the current date and publish date
$date_diff = ($current_timestamp - $publish_timestamp) / (60 * 60 * 24);
// Return 1 if the product is within the given number of days, otherwise return 0
return $date_diff <= $days ? 1 : 0;
}