
How to Display Custom WooCommerce Notice
- January 13, 2016
- Leave a comment
In WooCommerce forms, most of the clients need extensions in forms that best suit their business requirements. For instance, you may want to add a social security number along with certain registration fields. To save this extra field in the form, For saving this extra field in the form, you need to add notices/messages for the users.
Similarly, you may need to display error or success messages regarding a WooCommorce operation. You can use the wc_add_notice function to display these error messages on your custom pages/form. The WooCoomerce standard notice display function is as follows:
wc_add_notice($message, $notice_type);
It accepts two parameters:
- $message is the text to display in the notice
- $notice_type is the type of message you want to display
It also accepts three types of values:
- Error
- Notice
- Success
If you want to modify the templates of these messages, then you can modify the following files:
- templates/notice/error.php
- templates/notice/notice.php
- templates/notice/success.php
This is excellent… Except that it didn’t work. https://docs.woocommerce.com/wc-apidocs/function-wc_print_notice.html
Hello h8maus,
We have tested wc_add_notice function on our end. It’s working fine. You can print three different notices for your product. We are also sharing a code snippet as an example for you.
Thanks for reaching us out. Let us know if you need our further assistance.
Regards,
—
Abdullah | WP Plugin Developer
Email: support@presstigers.com
Hi, is it possible to add a custom notice_type?
I tried:
1. i copied woocommerce/templates/notices/success.php into mytheme/woocommerce/notices/warning.php
2. changed the css class to woocommerce-warning
3. added some CSS to customize the colors
4. added ‘woocommerce_notice_types’ filter to add ‘warning’ types to the print list
… and it’s working!
BUT it behave in a slighty different way than other notice_types: when updating cart my “warning” message is not updated, where ‘error’, ‘success’ and ‘notice’ types are updated.
Any ideas?
thanx!
Hello,
For WooCommerce customization, please contact to the Woo authors or on their support forum. They can guide you in a better and efficient way.
Thanks for reaching us out. Let us know if you need our further assistance.
Regards,
—
Abdullah | WP Plugin Developer
Email: support@presstigers.com
This also worked for me, the message appears on the /shop page above the product loop.
This ‘wp_print_notice’ on the other hand provides the same notice but where ever you happen to run the code:
wc_print_notice( $message, $notice_type = ‘success’ );