Skip to content
← Back to Snippets
Code

Abandoned Cart Detection Window

Finds carts that fall inside an abandoned-cart recovery window while excluding checked-out or suppressed carts.

Purpose

Finds carts that fall inside an abandoned-cart recovery window while excluding checked-out or suppressed carts.

Snippet details

ContextEcommerceLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Abandoned Cart Detection Window.
 *
 * Purpose:
 * Finds carts that fall inside an abandoned-cart recovery window while excluding checked-out or suppressed carts.
 *
 * @param array $cart_rows Cart rows.
 * @param int $min_age_minutes Minimum age before recovery.
 * @param int $max_age_minutes Maximum age before expiry.
 * @return array Abandoned carts eligible for recovery.
 */
function ogSnippetAbandonedCartDetectionWindow(array $cart_rows, int $min_age_minutes, int $max_age_minutes): array {
	$now = time();
	$eligible = array();
	foreach ($cart_rows as $cart) {
		$age_minutes = (int) floor(($now - (int) $cart['updated_at']) / 60);
		if ($cart['checked_out'] === false && $cart['suppressed'] === false && $age_minutes >= $min_age_minutes && $age_minutes <= $max_age_minutes) {
			$cart['age_minutes'] = $age_minutes;
			$eligible[] = $cart;
		}
	}
	return $eligible;
}

$abandoned = ogSnippetAbandonedCartDetectionWindow(array(array('cart_id' => 'CART-MILLER', 'updated_at' => time() - 7200, 'checked_out' => false, 'suppressed' => false)), 60, 4320);
echo count($abandoned);