What is the correct WooCommerce hook to use to modify notices after cart validation is completed and before wc_print_notices() is executed?
I’d like to customize the notices based on the totality of the notices to be printed.
The woocommerce_add_error filter runs as each individual error message is generated. The wc_get_notices() function returns the notices up to but not including the current notice, and it’s impossible to know which iteration will be the last.
I’m using a commercial cart plugin that replaces the woocommerce_before_cart action hook with a plugin_before_cart action hook.
When I hook into this action with a priority of 9, wc_notice_count() == 0 and wc_get_notices() returns an empty array.
When I hook into this action with a priority of 10, wc_print_notices() has already been executed.
What’s the correct hook to use?
Also, what is the replacement for the wc_add_notice() function?
This comment from the CartController.php file indicates that the use of wc_add_notice() is to be discouraged:
* This filter will be deprecated because it encourages usage of wc_add_notice. For the API we need to capture
* notices and convert to wp errors instead.
2
Answers
Thanks.
I was able to customize notices in their totality by editing the code in the purchased plugin and turning off updates for that plugin.
I replaced the action hook that calls 'woocommerce_output_all_notices' with a call to a function in my own plugin.
Not an ideal solution, but I couldn't figure out any other way to edit notices in their totality.
I particular, I'd prefer not to have multiple notices with identical phrasing.
Thanks again.
You can’t customize WooCommerce notices based on the totality of the notices to be printed, as the only hook available is
"woocommerce_add_{$notice_type}"
composite filter hook, that handle WooCommerce notices added via wc_add_notice() and wc_print_notice().Note: This composite hook will not work If the notices were bulk added via wc_set_notices().
Does wc_add_notice() is going to be replaced?
The CartController Class is part of StoreAPI, so only for WooCommerce Rest API.
It discourages the use of wc_add_notice() in favor of NoticeHandler Class and all Classes located in StoreApi > Exceptions folder.