Skip to content
Docs
Get RestroPress

Filter

rpress_cart_item_count

Updated
On this page

Filters the number of items in the cart, counting each line by its quantity.

Applied by rpress_get_cart_item_count(), which adds up the quantity of every cart line (a line without a quantity counts as 1). The value is shown by the cart and checkout templates and returned as item_count in AJAX cart responses. It does not change rpress_get_cart_quantity(), which counts cart lines. Changing it affects only the reported count, not the cart.

Available since RestroPress 3.5.1.

Parameters#

ParameterTypeDescription
$countintSum of the quantities of all cart lines.

Return value#

int. Item count. The result is cast to an integer.

Example#

add_filter( 'rpress_cart_item_count', 'my_cart_item_count', 10, 1 );
function my_cart_item_count( $count ) {
	foreach ( (array) rpress_get_cart_contents() as $item ) {
		if ( isset( $item['id'] ) && 123 === (int) $item['id'] ) {
			$count -= isset( $item['quantity'] ) ? max( 1, absint( $item['quantity'] ) ) : 1;
		}
	}
	return max( 0, $count );
}

Source#

Applied in:

  • includes/cart/functions.php (line 57), in rpress_get_cart_item_count()

See also#

Was this page helpful?