Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active March 17, 2022 17:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webaware/c6a6286026eb6a89e5a3 to your computer and use it in GitHub Desktop.
Save webaware/c6a6286026eb6a89e5a3 to your computer and use it in GitHub Desktop.
display WooCommerce notices in any page by shortcode [woocommerce_notices]
<?php
/*
Plugin Name: WooCommerce Notices Shortcode
Plugin URI: https://gist.github.com/webaware/c6a6286026eb6a89e5a3
Description: display WooCommerce notices in any page by shortcode [woocommerce_notices]
Author: WebAware
Author URI: https://shop.webaware.com.au/
*/
if (!defined('ABSPATH')) {
exit;
}
add_shortcode('woocommerce_notices', function($attrs) {
if (function_exists('wc_notice_count') && wc_notice_count() > 0) {
?>
<div class="woocommerce-notices-shortcode woocommerce">
<?php wc_print_notices(); ?>
</div>
<?php
}
});
@guyazran
Copy link

Just what i needed. Thanks!

@rasoulvatanparast
Copy link

Thanks for this great code. You are a great programmer.

@SteamDonkey
Copy link

This is awesome and we've been using it for some time. However, we recently ran into an issue where, if the short code is on a page, and Yoast settings are edited, the save produces an error.
Fatal error: Call to undefined function wc_notice_count()
The error doesn't happen if the short code isn't on the page when the Yoast setting changes are saved.
Any ideas on why this might be happening?

@webaware
Copy link
Author

@SteamDonkey please update with the current code, it should work OK now. It's because Yoast is doing its thing before WooCommerce has loaded everything.

@SteamDonkey
Copy link

blazingly fast fix and response, you're amazing, thank you!

@Waleedviews
Copy link

it doesn't work anymore. I try it on woocommerce Version 3.7.1

@lioncatde
Copy link

Does not work for me either. I really would appreciate if the code could be updated since it's exactly the function I need but can't find anywhere else

@xsonic
Copy link

xsonic commented Jul 29, 2020

Still works for me on 4.3.1. Great! Thank you!

@Yorlinq
Copy link

Yorlinq commented Dec 18, 2020

Works great. Thanx.

One question though: How can I change the position of the WooCommerce notice? I've noticed that no matter where I put the shortcode, the message will always appear on top of pretty much everything.

T.i.a.

@neilbannet
Copy link

You are angel :*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment