简体   繁体   English

设置和获取会话值wordpress

[英]Set and retrieve the session values wordpress

I'm using WooCommerce. 我正在使用WooCommerce。 I set my own session on page.php, and trying to pass that session to mini-cart.php, but mini-cart isn't receiving that session. 我在page.php上设置了自己的会话,并试图将该会话传递给mini-cart.php,但是mini-cart没有收到该会话。

page.php is like this page.php是这样的

get_header("english"); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
    <?php
    // Start the loop.
    // session_start();
    while ( have_posts() ) : the_post();

        // Include the page content template.
        get_template_part( 'template-parts/content', 'page' );

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }

        // End of the loop.
    endwhile;
    ?>

</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer();
// Adding my own session below
$_SESSION['menu_lang'] = "english";

I'm trying to print that session on mini-cart.php like this 我正在尝试像这样在mini-cart.php上打印该会话

if (!isset($_SESSION['menu_lang'])) {
 echo "no session";
} else {
 echo $_SESSION['menu_lang'];
}

How can I pass my session to mini-cart? 如何将我的会话传递给迷你购物车?

You can do using following code. 您可以使用以下代码来完成。

Add below code in your theme's functions.php file 在主题的functions.php文件中添加以下代码

function register_session_new(){
    if( ! session_id() ) {
       session_start();
     }
 }

add_action('init', 'register_session_new');
$_SESSION['menu_lang'] = "english";

Use this line in your mini-cart.php file or wherever you want to use. 在mini-cart.php文件中或任何想要使用的地方使用此行。

echo $_SESSION['menu_lang'] ;

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

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