rpress_get_cart_contents()

Author: bibhu 41 views

This function returns a nested array of items currently in the cart. Each item, by default, contains the following items:

  • id – The fooditem ID of the cart item
  • options – An array holding optional data, such as price_id (when variable pricing is enabled for the fooditem)
  • quantity – The quantity value. When quantities are disabled, this should always read 1

Common uses of the options array are things like variable pricing, software licensing upgrades & renewals, and recurring payments.

Example Usage

$cart_contents = rpress_get_cart_contents();
if ( ! empty( $contents ) ) {
	foreach ( $cart_items as $cart_item ) {
		// Execute your code
	}
}

Note: In our example, we assigned the contents of rpress_get_cart_contents() to a variable before checking it with the `empty` function in our conditional statement. This is to maintain compatibility with PHP 5.2.

Important Information

When called, this function does validate that each item in the cart meets two requirements.

  1. That it is of the post type `fooditem`.
  2. That the item is in a status that allows it to be purchased (non-editors cannot purchase posts that are not published).

If an item fails to meet either of those two requirements, it will be removed from the cart prior to returning the results.

Filters

return apply_filters( 'rpress_cart_contents', $cart );

It passes in one argument, which is the current cart contents after all validation has been done.