edd_get_cart_contents()
This function returns a nested array of items currently in the cart. Each item, by default, contains the following items:
- id – The download ID of the cart item
- options – An array holding optional data, such as price_id (when variable pricing is enabled for the download)
- 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 = edd_get_cart_contents(); if ( ! empty( $cart_contents ) ) { foreach ( $cart_contents as $item ) { // Execute your code } }
Note: In our example we assigned the contents of edd_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.
- That it is of the post type `download`.
- 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
This function contains one filter just before returning its results. The filter name is:
return apply_filters( 'edd_cart_contents', $cart );
It passes in one argument, which is the current cart contents, after all validation has been done.