[WooCommerce] Sale price dates in Bricks

A user asks:

Is there anyway to show the woo sale price dates (start/end) in bricks? I didn’t find any dynamic data for them in bricks builder!

Any custom way? I use pro countdown of bricks extra too

This Pro tutorial provides the steps for outputting start and end sale price dates (if set) for WooCommerce products when using Bricks.

Step 1

Set your shop’s timezone at Settings → General if you haven’t already. It is recommended to select a city in the same timezone as your business.

Step 2

Edit your products and set sale price dates by clicking Schedule under the Sale price input field.

Step 3

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

function bl_date_from_timestamp_field( string $field, string $format ): string {
    if ( $format === '' ) {
        $format = get_option( 'date_format' );
    }

    return ( new DateTimeImmutable() )->setTimestamp( get_post_meta( get_the_ID(), $field, true ) )->setTimezone( wp_timezone() )->format( $format );
}

We defined a custom function with 2 parameters. The first one is the name of the custom field (post meta key to be accurate) and the second one is the date format string like 'd M Y'.

If the format string is not supplied, we’ve set the date format in WordPress settings to be used. This is typically F j, Y by default.

The function creates a DateTimeImmutable object representing the date and time of the Unix timestamp, adjusted to the WordPress site’s timezone and returns it in the requested/site format.

Step 4

Whitelist the bl_date_from_timestamp_field function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_date_from_timestamp_field'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_date_from_timestamp_field. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 5

Edit the single product template with Bricks (or a products Bricks query loop if that’s where you want to show the sale price dates).

Display format of “Sale period: July 25, 2024 to July 30, 2024”

Add a Basic Text element and set its text to:


Sale period: {echo:bl_date_from_timestamp_field(_sale_price_dates_from,»)} to {echo:bl_date_from_timestamp_field(_sale_price_dates_to,»)}

view raw

gistfile1.txt

hosted with ❤ by GitHub

Apply a condition like this to ensure that it is output only for products that have both the start and end sale dates set:

{cf__sale_price_dates_from}

{cf__sale_price_dates_to}

Countdown format

Install and activate BricksExtras.

Enable Pro Countdown element in its settings.

Add a Pro Countdown element.

Countdown mode: Fixed end time

End date/time:

{echo:bl_date_from_timestamp_field(_sale_price_dates_to,'')}

Apply this condition:

{cf__sale_price_dates_to}

Reference

https://codexcoach.com/how-to-display-sale-price-end-date-in-woocommerce/