Php 在null上获取_cart()
在我更新到4.3.1版本的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
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购物车
。