Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在null上获取_cart()_Php_Wordpress_Woocommerce_Woocommerce Rest Api - Fatal编程技术网

Php 在null上获取_cart()

Php 在null上获取_cart(),php,wordpress,woocommerce,woocommerce-rest-api,Php,Wordpress,Woocommerce,Woocommerce Rest Api,在我更新到4.3.1版本的WC之前,我有这个代码,它运行得很好 add_action( 'rest_api_init', function () { register_rest_route( 'px-module-woocommerce', '/px/cart', array( 'methods' => 'POST', 'callback

在我更新到4.3.1版本的
WC
之前,我有这个代码,它运行得很好

 add_action( 'rest_api_init', function () {
                    register_rest_route( 'px-module-woocommerce', '/px/cart', array(
                            'methods' => 'POST',
                            'callback' => array($this,'ajax_add_to_cart'),
                    ));
});
public function ajax_add_to_cart() {
       $items = WC()->cart->get_cart();
}
现在,
WC()->cart->get\u cart()
仍然返回

Call to a member function get_cart() on null

我还尝试了全局值
$woocommerce
。但是,结果仍然是一样的。你有什么解决办法吗?谢谢。

我解决了只在前端加载的函数

public function ajax_add_to_cart() {
            include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
            include_once WC_ABSPATH . 'includes/class-wc-cart.php';

            if ( is_null( WC()->cart ) ) {
                wc_load_cart();
            }

           $items = WC()->cart->get_cart();
}

我在这方面没有太多的经验,但这篇推荐信对你有帮助吗:谢谢你的回复。是的,该文档非常有用,但仅描述
WC_Cart
类,并通过主类中的单例使用
WC()->购物车
=
WC\u购物车