Removing the Cart Item Key – API v1

This is a Developer level doc. We are unable to provide support for customizations under our  Support Policy. See guide on adding PHP code without editing files.

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.

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