Skip to content
← Back to Functions
Code

Discount Rule Evaluator

Applies coupon or automatic discount rules and returns explainable results.

Function signature

ogEvaluateDiscountRules(input = array(), options = array())

Categories

  • Ecommerce Workflows

Parameters

inputStructured workflow input array documented by this helper.optionsOptional documented policy controls for the helper.

Return value

Public-safe status string returned by the function for controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate file paths, routes, email tokens, cart totals, discount rules, and tax-region rules before production use.

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.
 */

/**
 * Applies coupon or automatic discount rules and returns explainable results.
 *
 * Primary use case: Ecommerce promotions.
 * Typical inputs: cart, customer, discount rules.
 * Typical output: discount report.
 *
 * Implementation note: Keep rule evaluation ordered and transparent.
 *
 * @param array $input Structured input values for this helper contract.
 * @param array $options Optional policy and formatting controls.
 * @return array Structured result data with success, message, and data keys.
 */
function ogEvaluateDiscountRules($input = array(), $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($input)) {
		$result['message'] = 'Input must be an array.';
		return $result;
	}

	if (!is_array($options)) {
		$options = array();
	}

	$cart_total = 0;
	if (!empty($input['cart_total_minor_units'])) {
		$cart_total = (int)$input['cart_total_minor_units'];
	}
	$rules = array();
	if (!empty($input['rules']) && is_array($input['rules'])) {
		$rules = $input['rules'];
	}
	$code = '';
	if (!empty($input['code'])) {
		$code = strtoupper(trim((string)$input['code']));
	}

	$applied = array();
	$total_discount = 0;
	foreach ($rules as $rule) {
		if (!is_array($rule)) {
			continue;
		}
		$rule_code = '';
		if (!empty($rule['code'])) {
			$rule_code = strtoupper(trim((string)$rule['code']));
		}
		if (!empty($rule_code) && !empty($code) && $rule_code != $code) {
			continue;
		}
		$minimum = 0;
		if (!empty($rule['minimum_minor_units'])) {
			$minimum = (int)$rule['minimum_minor_units'];
		}
		if ($cart_total < $minimum) {
			continue;
		}

		$amount = 0;
		if (!empty($rule['amount_minor_units'])) {
			$amount = (int)$rule['amount_minor_units'];
		}
		if (!empty($rule['percent'])) {
			$percent = (float)$rule['percent'];
			if ($percent > 0) {
				$amount += (int)floor($cart_total * ($percent / 100));
			}
		}
		if ($amount < 0) {
			$amount = 0;
		}
		$total_discount += $amount;
		$applied[] = array('code' => $rule_code, 'amount_minor_units' => $amount);
	}

	if ($total_discount > $cart_total) {
		$total_discount = $cart_total;
	}

	$result['success'] = true;
	$result['message'] = 'Discount rules evaluated.';
	$result['data'] = array(
		'applied' => $applied,
		'discount_minor_units' => $total_discount
	);

	return $result;
}