简体   繁体   中英

Woocommerce : how to display custom success notice message on Logout?

On Logout display success message. this is my code that i tried.but i don't get success message on logout

this is my code that i tried.but i don't get success message on logout

function displaynotice() {
    add_action( 'woocommerce_init', 'custom_notice' );
}
add_action('wp_logout', 'displaynotice');

function custom_notice() {
    wc_add_notice( 'This is a Success notice', 'success' );
}

I think when I logout, Wordpress or Woocommerce destroys the current session. So all my flash messages would also get destroyed or unset. So using session would not be an option.

Working solution

in function.php

setcookie('done', null, -1, '/');
add_action('wp_logout',function(){
   setcookie("done", "done", time() + (86400 * 30), "/");
});

and in the page where we want to show the message

<?php 
if(isset($_COOKIE["done"]) && !empty($_COOKIE["done"]) ) {?>
    <div class="woocommerce-message" role="alert">
    You've been logged out successfully.</div>
<?php } ?>

please try using wp_logout action hook, because whenever the user logs out this hook is called and you should add your logout method to this hook.

example :

add_action('wp_logout',function(){
    wc_add_notice( 'This is a Success notice', 'success' );
});

Working solution

in function.php

setcookie('done', null, -1, '/');
add_action('wp_logout',function(){
   setcookie("done", "done", time() + (86400 * 30), "/");
});

and in the page where we want to show the message

<?php 
if(isset($_COOKIE["done"]) && !empty($_COOKIE["done"]) ) {?>
    <div class="woocommerce-message" role="alert">
    You've been logged out successfully.</div>
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM