gpt4 book ai didi

php - WooCommerce get_cart() 为 null

转载 作者:行者123 更新时间:2023-12-04 14:12:34 25 4
gpt4 key购买 nike

在我更新到 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()还是回来
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();
}

关于php - WooCommerce get_cart() 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com