Fatal error: Call to a member function get_tax_class()
出现null错误,该错误指向class-wc-cart-totals.php的230行。似乎购物车项目数据属性为null。我不确定为什么,希望对WooCommerce的胆量有更深入了解的人能为我指明正确的方向。
这里的设置有点独特,我创建了自定义API端点来处理来自客户端的大多数WooCommerce交互。
尝试更新商品数量时出现此错误。
/*Cart controls*/
function my_add_to_cart($request){
$payload = $request->get_params();
if ($payload['product_id']){
$cart_item_key = WC()->cart->add_to_cart( $payload['product_id'], $payload['quantity'] );
}
return my_get_cart()[$cart_item_key];
}
function my_remove_cart_item($request){
$payload = $request->get_params();
if ($payload['cart_item_id']){
$payload['success'] = WC()->cart->remove_cart_item( $payload['cart_item_id'] );
} else {
$payload['success'] = false;
}
return $payload;
}
function my_update_cart_item_quantity($request){
$payload = $request->get_params();
if ($payload['cart_item_id']){
/*This is where the error is triggered in my API */
$payload['success'] = WC()->cart->set_quantity( $payload['id'], $payload['quantity'], true );
} else {
$payload['success'] = false;
}
return $payload;
}
内部错误发生在这里:
get_items_from_cart(){
$this->items = array();
foreach ( $this->cart->get_cart() as $cart_item_key => $cart_item ) {
$item = $this->get_default_item_props();
$item->key = $cart_item_key;
$item->object = $cart_item;
$item->tax_class = $cart_item['data']->get_tax_class();
/*This line here*/
$item->taxable = 'taxable' === $cart_item['data']->get_tax_status();
$item->price_includes_tax = wc_prices_include_tax();
$item->quantity = $cart_item['quantity'];
$item->price = wc_add_number_precision_deep( $cart_item['data']->get_price() * $cart_item['quantity'] );
$item->product = $cart_item['data'];
$item->tax_rates = $this->get_item_tax_rates( $item );
$this->items[ $cart_item_key ] = $item;
}
}
显然$cart_item['data']
在这里没有正确实例化,但是我不明白为什么。
我现在有点不知所措。我希望我做过一些明显错误的事情,并且可以在我对开发人员进行可能的错误修复之前解决。.错误...
注意:通过我的API添加和删除项的操作就像一个超级按钮。
答案 0 :(得分:0)
一个小时的调试和整个发布之后,我在35秒后发现参数不匹配:
$payload['success'] = WC()->cart->set_quantity( $payload['id'], $payload['quantity'], true );
需要成为
$payload['success'] = WC()->cart->set_quantity( $payload['cart_item_id'], $payload['quantity'], true );