Display a notice on the single WooCommerce product page

In today’s article we will add a small banner on the single product page which will urge our customers to complete their orders as quickly as possible to take advantage of same day shipping.

Install and activate a child theme

The first step on our process here is to create and install a child theme. If you are using one of our themes you can easily grab its child theme from our downloads section. If not, you can read our beginner’s guide on child themes to create your own. This step is essential in order to preserve our changes throughout theme updates.

All code below will be placed in our child theme’s functions.php file. If the file is empty the code will go just below the opening <?php tag, otherwise, if it has contents, the code should be placed at the end of the file, before the closing ?> PHP tag if it exists.

Display the banner

To display our banner we will use the woocommerce_single_product_summary hook. By default WooCommerce has the product title, rating, price, excerpt, meta and sharing options hooked with priorities of 5, 10, 10(?), 20, 40 and 50 respectively. Let’s show our banner below the price and use a priority of 19.

add_action( 'woocommerce_single_product_summary', 'cssigniter_display_banner_below_single_title', 19 );
function cssigniter_display_banner_below_single_title() {
	ob_start(); ?>
		<div class="woocommerce-message">
			<?php esc_html_e( 'This is a great banner', 'your-text-domain' ); ?>
		</div>
	<?php
	echo ob_get_clean();
}

Now we have a banner with our text which appears on all products below the price. Let’s take it a step further and say we want to let our customers know of a same day shipping possibility if they order before a certain time.

add_action( 'woocommerce_single_product_summary', 'cssigniter_display_banner_below_single_title', 19 );
function cssigniter_display_banner_below_single_title() {

	$cur_dt    = current_datetime();
	$weekday   = absint( $cur_dt->format( 'N' ) );
	$localtime = $cur_dt->getTimestamp() + $cur_dt->getOffset();
	$noon      = new DateTime( 'today noon', wp_timezone() );
	$localnoon = $noon->getTimestamp() + $noon->getOffset();

	if ( ( $localtime >= $localnoon ) || in_array( $weekday, array( 6, 7 ), true ) ) {
		return;
	}

	ob_start();
	?>
		<div class="woocommerce-message">
			<?php esc_html_e( 'Order now to get it shipped today!', 'your-text-domain' ); ?>
		</div>
	<?php
	echo ob_get_clean();
}

Our snippet above will output a message that urges our customers to order now in order to have the product shipped to them today. The snippet will only display the message if the store’s local time is before noon (12pm) and if it is not a Saturday or Sunday. You can have a look at PHP’s DateTime object format if you want to fiddle around with the $noon variable which returns a date object of the current day’s noon on the UTC timezone.

Wrapping up

With this small snippet we can easily add a banner to all our products informing users of a service like the same day shipping in the example above, or a store wide offer and anything else we might want our customers to notice. With an easy modification on the dates we can set it to display a limited time offer and have it disappear once the offer period passes, without having to worry about removing it manually. Did you find this snippet useful? Let us know in the comments below.

Related Articles

2 comments

  1. Morgan says:

    Wow great,
    I think i will have huge benefit if im using your theme since i can found many usefull content like this.

    I want to build a listing website using ‘listee’ and have been consider to buy a package of your bundle.

    But i will also have a feature for user to make an event listing, what event plugin best to include on listee theme to acommodate my goal ?

    1. Nik says:

      Hello Morgan.
      Any popular event management plugin for WordPress should do the trick. Listee only has some affects on WP Job Manager and WooCommerce due to the provided integration. Other plugins should work as expected. You could run into styling issues in some rare cases but our support can help you fix them.

Leave a Reply

Your email address will not be published.