WooCommerce WC() - > session-> get无法识别 [英] WooCommerce WC()->session->get is not getting recognized

查看:306
本文介绍了WooCommerce WC() - > session-> get无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个网站,其中用户输入优惠券代码,并通过以 couponid 的名称创建Cookie来登录优惠券代码,然后将该Cookie保存到WooCommerce会话,因为该Cookie实际上是显示产品的类别ID。

We have created a site where a user enters a coupon code and it logs them in by creating a cookie by the name of couponid, then we save that cookie to the WooCommerce session because that cookie actually is the category id from which the products are displayed.

现在有时产品会显示,有时不会显示,浏览器和我们去 / wp-admin 登录,它给我们以下错误:

Now sometimes the products get displayed, sometimes not, and whenever our custom cookie is set in the browser and we go to /wp-admin to login, it gives us the following error:


致命错误:调用非对象的成员函数get()

Fatal error: Call to a member function get() on a non-object

登录时出现上述错误WordPress的屏幕来自我们的 functions.php 文件中的以下函数:

The above error on the login screen of WordPress is coming from the following function in our functions.php file:

function gfc_insert_coupon_code_to_session(){

if( 
    is_user_logged_in() 
    || ! array_key_exists( 'couponid', $_COOKIE ) 
    || WC()->session->get( 'couponid', 0 )
){
    return;
}

$couponID = esc_attr( $_COOKIE['couponid'] );

    if( $couponID ){
        WC()->session->set( 'couponid', $couponID );
    }
}

add_action( 'woocommerce_init', 'gfc_insert_coupon_code_to_session' );


推荐答案

尝试使用:

WC()->session->set( 'couponid', $couponID );

调用之前:

WC()->session->get( 'couponid', 0 )

这篇关于WooCommerce WC() - > session-> get无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆