简体   繁体   English

WooCommerce-尝试更新数量时购物车项目数据为空

[英]WooCommerce - cart item data null when attempting to update quantity

Fatal error: Call to a member function get_tax_class() on null error that points to Line 230 of class-wc-cart-totals.php. Fatal error: Call to a member function get_tax_class()在指向class-wc-cart-totals.php的第230行的空错误上Fatal error: Call to a member function get_tax_class() It appears the cart item data property is null. 似乎购物车项目数据属性为null。 I am unsure why, hoping someone with better insights into the guts of WooCommerce can point me in the right direction. 我不确定为什么,希望对WooCommerce的胆量有更深入了解的人能为我指明正确的方向。

Slightly unique setup here, I've created custom API endpoints to handle most of the WooCommerce interactions from the client side. 在这里,设置有些许独特,我创建了自定义API端点来处理来自客户端的大多数WooCommerce交互。

I'm getting this error when trying to update the quantity of an item. 尝试更新商品数量时出现此错误。

/*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;
}

And the internal error happens here: 内部错误发生在这里:

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;
    }
}

So obviously $cart_item['data'] isn't properly instantiated here, but I don't understand why. 因此,显然$cart_item['data']在此处未正确实例化,但我不明白为什么。

I'm just kind of at a loss at this point. 在这一点上,我有点茫然。 I'm hoping there's something I've done glaringly wrong and can fix before I go bug the devs about a possible.. bug... 我希望有些事情我做得很明显是错误的,并且可以在我对开发人员进行调试之前解决。

Note: adding and removing items via my API works like a charm. 注意:通过我的API添加和删除项目的过程就像一个超级按钮。

An hour of debugging and this whole post, it's a mismatched parameter I find 35 seconds later: 一个小时的调试和整个发布后,我在35秒后发现参数不匹配:

   $payload['success'] = WC()->cart->set_quantity( $payload['id'], $payload['quantity'], true );

needs to be 需要是

   $payload['success'] = WC()->cart->set_quantity( $payload['cart_item_id'], $payload['quantity'], true );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM