简体   繁体   English

Woocommerce:如何在注销时显示自定义成功通知消息?

[英]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.我认为当我注销时,Wordpress 或 Woocommerce 会破坏当前会话。 So all my flash messages would also get destroyed or unset.因此,我所有的 Flash 消息也会被销毁或取消设置。 So using session would not be an option.所以使用 session 不是一种选择。

Working solution工作解决方案

in function.php在函数.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.请尝试使用wp_logout操作钩子,因为每当用户注销时,都会调用此钩子,您应该将注销方法添加到此钩子中。

example :例子 :

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

Working solution工作解决方案

in function.php在函数.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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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