What you see when you return the cart contents is how WooCommerce formats the data. This format is what WooCommerce or a WooCommerce extension use to be able to identify the item in cart should they need to alter it before or after being added to the cart or even update it.
However, via the REST API this particular key is not a requirement to have as the parent of the array of item data and since we are not altering the core of WooCommerce, we can remove it.
<?php /** * Returns the cart contents without the cart item key as the parent array. * * @param array $cart_contents - Before modifying the cart response. * @return array $new_cart_contents - After modifying the cart response. */ function remove_parent_cart_item_key( $cart_contents ) { $new_cart_contents = array(); foreach ( $cart_contents as $item_key => $cart_item ) { $new_cart_contents[] = $cart_item; } return $new_cart_contents; } add_filter( 'cocart_return_cart_contents', 'remove_parent_cart_item_key', 0 );
Was this helpful to you?
Did you like the article/guide or would you like to give some feedback? Click on one of the buttons and let us know your opinion!