WooCommerce get_cart() 为空
WooCommerce get_cart() on null
在我更新到 WC
4.3.1 之前,我有这段代码并且运行良好
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_cart()
还是return
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
4.3.1 之前,我有这段代码并且运行良好
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_cart()
还是return
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();
}