WooCommerce Product "New" Label

As described here (WooCommerce Builder – Bricks Academy), there is the “new” label.

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?

I believe this is not currently possible outside of the products element. However, this is rather easily done with a few lines of code: WooCommerce: Display "New!" Badge on Recent Products

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?

@timmse Is there a way to access the function that Bricks uses?

I have now solved it with my own function. But I don’t think it’s good to have an extra function, even though this function already exists.

I would like to push the topic again. Is there a way to access the “New” function of Bricks?

Hopefully a solution to show the “new” label on the query loop is added to the bricks.

1 Like

label in Bricks without the Bricks Products element

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.

Quite simple to achieve.

Add this to your functions.php file:

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:

Screenshot 2024-08-25 143440

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;
}

Then you can set the days here:

Screenshot 2024-08-25 144145