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

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

问题描述

我们创建了一个网站,用户在其中输入优惠券代码,并通过创建名称为 couponid 的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.

现在有时会显示产品,有时会不显示产品,并且只要在浏览器中设置了我们的自定义cookie并进入/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()->会话-> get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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