| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Email; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class ConditionPresets |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Get all condition presets. |
| 11 |
* |
| 12 |
* Each preset has: id, label, shortcode, condition, compareValue |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public static function all() |
| 17 |
{ |
| 18 |
$presets = [ |
| 19 |
// Order Type |
| 20 |
[ |
| 21 |
'id' => 'is_digital_order', |
| 22 |
'label' => __('Is Digital Order', 'fluent-cart'), |
| 23 |
'hint' => __('Show when the order contains only digital products.', 'fluent-cart'), |
| 24 |
'shortcode' => '{{order.is_digital}}', |
| 25 |
'condition' => 'equal', |
| 26 |
'compareValue' => 'yes', |
| 27 |
], |
| 28 |
[ |
| 29 |
'id' => 'is_physical_order', |
| 30 |
'label' => __('Is Physical Order', 'fluent-cart'), |
| 31 |
'hint' => __('Show when the order requires physical fulfillment.', 'fluent-cart'), |
| 32 |
'shortcode' => '{{order.is_digital}}', |
| 33 |
'condition' => 'equal', |
| 34 |
'compareValue' => 'no', |
| 35 |
], |
| 36 |
|
| 37 |
// Payment Status |
| 38 |
[ |
| 39 |
'id' => 'is_order_paid', |
| 40 |
'label' => __('Is Order Paid', 'fluent-cart'), |
| 41 |
'hint' => __('Show when payment has been received.', 'fluent-cart'), |
| 42 |
'shortcode' => '{{order.payment_status}}', |
| 43 |
'condition' => 'equal', |
| 44 |
'compareValue' => 'paid', |
| 45 |
], |
| 46 |
[ |
| 47 |
'id' => 'is_order_pending', |
| 48 |
'label' => __('Is Order Pending', 'fluent-cart'), |
| 49 |
'hint' => __('Show when payment is still pending.', 'fluent-cart'), |
| 50 |
'shortcode' => '{{order.payment_status}}', |
| 51 |
'condition' => 'equal', |
| 52 |
'compareValue' => 'pending', |
| 53 |
], |
| 54 |
[ |
| 55 |
'id' => 'is_order_failed', |
| 56 |
'label' => __('Is Order Failed', 'fluent-cart'), |
| 57 |
'hint' => __('Show when payment has failed.', 'fluent-cart'), |
| 58 |
'shortcode' => '{{order.payment_status}}', |
| 59 |
'condition' => 'equal', |
| 60 |
'compareValue' => 'failed', |
| 61 |
], |
| 62 |
|
| 63 |
// Financials |
| 64 |
[ |
| 65 |
'id' => 'has_discount', |
| 66 |
'label' => __('Has Discount', 'fluent-cart'), |
| 67 |
'hint' => __('Show when a coupon or manual discount was applied.', 'fluent-cart'), |
| 68 |
'shortcode' => '{{order.discount_total}}', |
| 69 |
'condition' => 'greater_than', |
| 70 |
'compareValue' => '0', |
| 71 |
], |
| 72 |
[ |
| 73 |
'id' => 'has_shipping', |
| 74 |
'label' => __('Has Shipping', 'fluent-cart'), |
| 75 |
'hint' => __('Show when shipping charges are present.', 'fluent-cart'), |
| 76 |
'shortcode' => '{{order.shipping_total}}', |
| 77 |
'condition' => 'greater_than', |
| 78 |
'compareValue' => '0', |
| 79 |
], |
| 80 |
[ |
| 81 |
'id' => 'has_tax', |
| 82 |
'label' => __('Has Tax', 'fluent-cart'), |
| 83 |
'hint' => __('Show when the order includes tax.', 'fluent-cart'), |
| 84 |
'shortcode' => '{{order.tax_total}}', |
| 85 |
'condition' => 'greater_than', |
| 86 |
'compareValue' => '0', |
| 87 |
], |
| 88 |
[ |
| 89 |
'id' => 'has_refund', |
| 90 |
'label' => __('Has Refund', 'fluent-cart'), |
| 91 |
'hint' => __('Show when a refund has been issued.', 'fluent-cart'), |
| 92 |
'shortcode' => '{{order.total_refund}}', |
| 93 |
'condition' => 'greater_than', |
| 94 |
'compareValue' => '0', |
| 95 |
], |
| 96 |
[ |
| 97 |
'id' => 'has_shipping_tax', |
| 98 |
'label' => __('Has Shipping Tax', 'fluent-cart'), |
| 99 |
'hint' => __('Show when shipping tax is charged.', 'fluent-cart'), |
| 100 |
'shortcode' => '{{order.shipping_tax}}', |
| 101 |
'condition' => 'greater_than', |
| 102 |
'compareValue' => '0', |
| 103 |
], |
| 104 |
|
| 105 |
// Content |
| 106 |
[ |
| 107 |
'id' => 'has_order_note', |
| 108 |
'label' => __('Has Order Note', 'fluent-cart'), |
| 109 |
'hint' => __('Show when the customer left an order note.', 'fluent-cart'), |
| 110 |
'shortcode' => '{{order.note}}', |
| 111 |
'condition' => 'not_empty', |
| 112 |
'compareValue' => '', |
| 113 |
], |
| 114 |
[ |
| 115 |
'id' => 'has_downloads', |
| 116 |
'label' => __('Has Downloads', 'fluent-cart'), |
| 117 |
'hint' => __('Show when downloadable files are attached.', 'fluent-cart'), |
| 118 |
'shortcode' => '{{order.downloads}}', |
| 119 |
'condition' => 'not_empty', |
| 120 |
'compareValue' => '', |
| 121 |
], |
| 122 |
]; |
| 123 |
|
| 124 |
return apply_filters('fluent_cart/condition_presets', $presets); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get presets formatted for JS localization (id + label only). |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public static function forJs() |
| 133 |
{ |
| 134 |
return array_map(function ($preset) { |
| 135 |
return [ |
| 136 |
'id' => Arr::get($preset, 'id'), |
| 137 |
'label' => Arr::get($preset, 'label'), |
| 138 |
'hint' => Arr::get($preset, 'hint', ''), |
| 139 |
]; |
| 140 |
}, static::all()); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Find a preset by its identifier. |
| 145 |
* |
| 146 |
* @param string $id |
| 147 |
* @return array|null |
| 148 |
*/ |
| 149 |
public static function find($id) |
| 150 |
{ |
| 151 |
foreach (static::all() as $preset) { |
| 152 |
if (Arr::get($preset, 'id') === $id) { |
| 153 |
return $preset; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Resolve a preset ID to its evaluation parameters. |
| 162 |
* |
| 163 |
* Returns one of three shapes: |
| 164 |
* - callback present: ['type' => 'callback', 'callback' => callable, 'presetId' => string] |
| 165 |
* - shortcode present: ['type' => 'shortcode', 'shortcode' => string, 'condition' => string, 'compareValue' => string] |
| 166 |
* - neither: ['type' => 'filter', 'presetId' => string] |
| 167 |
* |
| 168 |
* @param string $presetId |
| 169 |
* @return array|null |
| 170 |
*/ |
| 171 |
public static function resolve($presetId) |
| 172 |
{ |
| 173 |
$preset = static::find($presetId); |
| 174 |
|
| 175 |
if (!$preset) { |
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
$callback = Arr::get($preset, 'callback'); |
| 180 |
|
| 181 |
if ($callback && is_callable($callback)) { |
| 182 |
return [ |
| 183 |
'callback' => $callback, |
| 184 |
]; |
| 185 |
} |
| 186 |
|
| 187 |
return [ |
| 188 |
'shortcode' => Arr::get($preset, 'shortcode', ''), |
| 189 |
'condition' => Arr::get($preset, 'condition', 'not_empty'), |
| 190 |
'compareValue' => Arr::get($preset, 'compareValue', ''), |
| 191 |
]; |
| 192 |
} |
| 193 |
} |
| 194 |
|