【问题标题】:How to load woocommerce get_session_cookie on page load in WordPress?如何在 WordPress 页面加载时加载 woocommerce get_session_cookie?
【发布时间】:2018-08-01 10:22:51
【问题描述】:

我在没有刷新页面的情况下在 WordPress 中签出订单。

不刷新页面的单页订单的要求之一是初始化 woocommerce get_session_cookie()。

get_session_cookie() 在 class-wc-handler.php 文件中找到,里面是一个名为class WC_Session_Handler extends WC_Session {}的类

function get_my_wc_session_cookie() {
    WC_Session_Handler::get_session_cookie();
}

add_action('init', 'get_my_wc_session_cookie');

我在functions.php中添加了get_session_cookie()函数并将其挂钩到'init'

但我提示错误:

致命错误:当不在对象上下文中时使用 $this C:\XamppDev\htdocs\mywebsite\wp-content\plugins\woocommerce\includes\class-wc-session-handler.php 在第 170 行

您知道如何在页面加载时从 woocommerce 加载 get_session_cookie() 吗?

【问题讨论】:

    标签: javascript php jquery html wordpress


    【解决方案1】:
    My friend try to set the cookie and then get cookie. sometimes at local system woocommerce cookie function desn't give appropriate result.
    Please set a cookie by using the function below in functions.php --
    
    <?php
    add_action( 'init', 'setting_my_first_cookie' );
    
    function setting_my_first_cookie() {
     setcookie( $v_username, $v_value, 30 * DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
    }
    ?>
    
    Get the Cookie as below --
    
    <?php
    
    if(!isset($_COOKIE[$v_username])) {
      echo "The cookie: '" . $v_username . "' is not set.";
    } else {
      echo "The cookie '" . $v_username . "' is set.";
      echo "Cookie is:  " . $_COOKIE[$v_username];
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      我可以用下面的代码解决这个问题:

      <?php
      
        WC()->session->set_customer_session_cookie(true);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多