'wc_print_notices is not displaying any messages in woocommerce pages?

I have used default woocommerce templates for shop, cart and single page. I have not removed any hooks either but also I am not getting any message. Any Idea?

add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
   echo 'Hook is working fine';
}

I am getting this message 'Hook is working fine' but not wc_print_notices();.



Solution 1:[1]

I'm not really sure what is the problem. Your question needs further details. With that said, can you try adding this code to your functions.php of your current theme.

add_action( 'template_redirect', 'test' );
function test() {

    wc_add_notice( __( 'Sorry there was a problem.', 'woocommerce' ), 'error' );

}

Let me know if it does something.


UPDATE

If you have something like this:

add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 10 );
function Cusotm_wc_print_notices()
{
   $notices = WC()->session->get('wc_notices');
   print_r($notices);
}

it will not work because $notices will be empty once wc_print_notices() is called.

try changing priority and you will get something. Should be something like this:

add_action( 'woocommerce_before_single_product',   'Cusotm_wc_print_notices', 9 ); 

reigelgallarde.me
use priority below 10. Because WooCommerce is using 10.

add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1