Randy Hall
Randy Hall

Reputation: 8127

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. It appears the cart item data property is null. I am unsure why, hoping someone with better insights into the guts of WooCommerce can point me in the right direction.

Slightly unique setup here, I've created custom API endpoints to handle most of the WooCommerce interactions from the client side.

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.

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.

Upvotes: 1

Views: 1397

Answers (1)

Randy Hall
Randy Hall

Reputation: 8127

An hour of debugging and this whole post, it's a mismatched parameter I find 35 seconds later:

   $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 );

Upvotes: 1

Related Questions