| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\Api\CurrencySettings; |
| 6 |
use FluentCart\Api\StoreSettings; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Helpers\Status; |
| 9 |
use FluentCart\App\Models\Order; |
| 10 |
use FluentCart\App\Models\Subscription; |
| 11 |
use FluentCart\App\Services\DateTime\DateTime; |
| 12 |
use FluentCart\App\Services\Payments\PaymentReceipt; |
| 13 |
use FluentCart\App\Services\TemplateService; |
| 14 |
use FluentCart\Framework\Support\Arr; |
| 15 |
use FluentCart\Framework\Support\Str; |
| 16 |
use FluentCartPro\App\Modules\Licensing\Models\License; |
| 17 |
|
| 18 |
class OrderParser extends BaseParser |
| 19 |
{ |
| 20 |
private StoreSettings $storeSettings; |
| 21 |
private $order; |
| 22 |
private $orderTz; |
| 23 |
|
| 24 |
private $licenses; |
| 25 |
|
| 26 |
private bool $licenseLoaded = false; |
| 27 |
|
| 28 |
private $subscriptions; |
| 29 |
|
| 30 |
private bool $subscriptionLoaded = false; |
| 31 |
|
| 32 |
public function __construct($data) |
| 33 |
{ |
| 34 |
$this->storeSettings = new StoreSettings(); |
| 35 |
$this->order = Arr::get($data, 'order'); |
| 36 |
$config = Arr::wrap( |
| 37 |
Arr::get($this->order, 'config') |
| 38 |
); |
| 39 |
$rawTz = Arr::get($config, 'user_tz', 'UTC'); |
| 40 |
$this->orderTz = (@timezone_open($rawTz) !== false) ? $rawTz : 'UTC'; |
| 41 |
$orderId = Arr::get($this->order, 'id'); |
| 42 |
|
| 43 |
|
| 44 |
parent::__construct($data); |
| 45 |
} |
| 46 |
|
| 47 |
// protected array $methodMap = [ |
| 48 |
// 'customer_dashboard_link' => 'getCustomerDashboardLink', |
| 49 |
// 'payment_summary' => 'getPaymentSummary', |
| 50 |
// 'payment_receipt' => 'getPaymentReceipt', |
| 51 |
// ]; |
| 52 |
|
| 53 |
protected array $methodMap = [ |
| 54 |
'item_count' => 'getItemCount', |
| 55 |
'is_digital' => 'getIsDigital', |
| 56 |
]; |
| 57 |
|
| 58 |
protected array $attributeMap = [ |
| 59 |
'id' => 'order.id', |
| 60 |
'status' => 'order.status', |
| 61 |
'created_at' => 'order.created_at', |
| 62 |
'updated_at' => 'order.updated_at', |
| 63 |
]; |
| 64 |
|
| 65 |
protected array $centColumns = [ |
| 66 |
'total_amount', |
| 67 |
'subtotal', |
| 68 |
'discount_tax', |
| 69 |
'manual_discount_total', |
| 70 |
'coupon_discount_total', |
| 71 |
'shipping_tax', |
| 72 |
'shipping_total', |
| 73 |
'fee_total', |
| 74 |
'tax_total', |
| 75 |
'total_paid', |
| 76 |
'total_refund' |
| 77 |
]; |
| 78 |
|
| 79 |
public function parse($accessor = '', $code = '', $transformer = null): ?string |
| 80 |
{ |
| 81 |
|
| 82 |
if ($this->shouldParseAddress($accessor)) { |
| 83 |
return $this->parseAddressFields($accessor); |
| 84 |
} |
| 85 |
|
| 86 |
if (in_array($accessor, ['updated_at', 'created_at'])) { |
| 87 |
$date = Arr::get($this->data, $this->attributeMap[$accessor]); |
| 88 |
$timestamp = DateTime::anyTimeToGmt($date)->getTimestamp(); |
| 89 |
|
| 90 |
$date = wp_date( |
| 91 |
get_option('date_format'), |
| 92 |
$timestamp, |
| 93 |
new \DateTimeZone($this->orderTz) |
| 94 |
); |
| 95 |
|
| 96 |
return Helper::translateNumber($date); |
| 97 |
} |
| 98 |
|
| 99 |
// Handle _formatted suffix for cent columns (e.g. total_amount_formatted) |
| 100 |
$formattedSuffix = '_formatted'; |
| 101 |
if (Str::endsWith($accessor, $formattedSuffix)) { |
| 102 |
$baseAccessor = substr($accessor, 0, -strlen($formattedSuffix)); |
| 103 |
if (in_array($baseAccessor, $this->centColumns)) { |
| 104 |
$amount = Arr::get($this->order, $baseAccessor); |
| 105 |
if (!is_numeric($amount)) { |
| 106 |
return (string) $amount; |
| 107 |
} |
| 108 |
return CurrencySettings::getPriceHtml($amount, $this->order['currency']); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (in_array($accessor, $this->centColumns)) { |
| 113 |
$amount = Arr::get($this->order, $accessor); |
| 114 |
if (!is_numeric($amount)) { |
| 115 |
return (string) $amount; |
| 116 |
} |
| 117 |
return (string) ($amount / 100); |
| 118 |
} |
| 119 |
|
| 120 |
// $html parsers |
| 121 |
$htmlParsers = [ |
| 122 |
'order.download_details', |
| 123 |
'order.items_table', |
| 124 |
'order.payment_summary', |
| 125 |
'order.payment_receipt', |
| 126 |
'order.subscription_details', |
| 127 |
'order.license_details', |
| 128 |
'order.address_details', |
| 129 |
]; |
| 130 |
|
| 131 |
if (in_array($code, $htmlParsers)) { |
| 132 |
|
| 133 |
$order = $this->order; |
| 134 |
|
| 135 |
if ($code == 'order.items_table') { |
| 136 |
return \FluentCart\App\App::make('view')->make('emails.parts.items_table', [ |
| 137 |
'order' => $order, |
| 138 |
'formattedItems' => $order->order_items, |
| 139 |
'heading' => __('Order Summary', 'fluent-cart'), |
| 140 |
]); |
| 141 |
} |
| 142 |
|
| 143 |
if ($code === 'order.subscription_details') { |
| 144 |
if ($order->subscriptions && $order->subscriptions->count() > 0) { |
| 145 |
return \FluentCart\App\App::make('view')->make('invoice.parts.subscription_items', [ |
| 146 |
'subscriptions' => $order->subscriptions, |
| 147 |
'order' => $order |
| 148 |
]); |
| 149 |
} |
| 150 |
return ''; |
| 151 |
} |
| 152 |
|
| 153 |
if ($code === 'order.license_details') { |
| 154 |
$licenses = $order->getLicenses(); |
| 155 |
if ($licenses && $licenses->count() > 0) { |
| 156 |
return \FluentCart\App\App::make('view')->make('emails.parts.licenses', [ |
| 157 |
'licenses' => $licenses, |
| 158 |
'heading' => _n('License', 'Licenses', $licenses->count(), 'fluent-cart'), |
| 159 |
'show_notice' => false |
| 160 |
]); |
| 161 |
} |
| 162 |
return ''; |
| 163 |
} |
| 164 |
|
| 165 |
if ($code === 'order.download_details') { |
| 166 |
$downloads = $order->getDownloads(); |
| 167 |
if ($downloads) { |
| 168 |
return \FluentCart\App\App::make('view')->make('emails.parts.downloads', [ |
| 169 |
'order' => $order, |
| 170 |
'heading' => _n('Download', 'Downloads', count($downloads), 'fluent-cart'), |
| 171 |
'downloadItems' => $downloads, |
| 172 |
]); |
| 173 |
} |
| 174 |
return ''; |
| 175 |
} |
| 176 |
|
| 177 |
if ($code === 'order.address_details') { |
| 178 |
return \FluentCart\App\App::make('view')->make('emails.parts.addresses', [ |
| 179 |
'order' => $order, |
| 180 |
]); |
| 181 |
} |
| 182 |
|
| 183 |
if ($code == 'order.payment_summary') { |
| 184 |
return $this->getPaymentSummary(); |
| 185 |
} |
| 186 |
if ($code == 'order.payment_receipt') { |
| 187 |
return $this->getPaymentReceipt(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
return $this->get($accessor, $code); |
| 193 |
} |
| 194 |
|
| 195 |
public function shouldParseAddress($accessor): bool |
| 196 |
{ |
| 197 |
return Str::startsWith($accessor, 'billing.') || Str::startsWith($accessor, 'shipping.'); |
| 198 |
} |
| 199 |
|
| 200 |
public function parseAddressFields($accessor) |
| 201 |
{ |
| 202 |
list($addressType, $accessorsKey) = $this->resolveAddressFieldKeys($accessor); |
| 203 |
return $this->getAddressData($addressType, $accessorsKey); |
| 204 |
} |
| 205 |
|
| 206 |
public function resolveAddressFieldKeys($accessor): array |
| 207 |
{ |
| 208 |
$exploded = explode('.', $accessor); |
| 209 |
$addressType = $exploded[0]; |
| 210 |
$accessorsKey = implode('.', array_slice($exploded, 1)); |
| 211 |
return [$addressType, $accessorsKey]; |
| 212 |
} |
| 213 |
|
| 214 |
public function getAddressData($addressAccessor, $accessor = null) |
| 215 |
{ |
| 216 |
$address = Arr::get($this->order, $addressAccessor . '_address'); |
| 217 |
|
| 218 |
if (empty($address)) { |
| 219 |
return ""; |
| 220 |
} |
| 221 |
|
| 222 |
$formattedFields = ['city', 'state', 'country']; |
| 223 |
if (in_array($accessor, $formattedFields) && method_exists($address, 'getFormattedAddress')) { |
| 224 |
$formatted = $address->getFormattedAddress(); |
| 225 |
return Arr::get($formatted, $accessor) ?: ''; |
| 226 |
} |
| 227 |
|
| 228 |
return Arr::get($address, $accessor) ?: ''; |
| 229 |
} |
| 230 |
|
| 231 |
public function getPaymentSummary() |
| 232 |
{ |
| 233 |
$order = $this->order; |
| 234 |
|
| 235 |
return \FluentCart\App\App::make('view')->make('emails.parts.items_table', [ |
| 236 |
'order' => $order, |
| 237 |
'formattedItems' => $order->order_items, |
| 238 |
'heading' => '', |
| 239 |
]); |
| 240 |
} |
| 241 |
|
| 242 |
public function getPaymentReceipt() |
| 243 |
{ |
| 244 |
$order = $this->order; |
| 245 |
|
| 246 |
ob_start(); |
| 247 |
|
| 248 |
\FluentCart\App\App::make('view')->render('emails.parts.items_table', [ |
| 249 |
'order' => $order, |
| 250 |
'formattedItems' => $order->order_items, |
| 251 |
'heading' => __('Order Summary', 'fluent-cart'), |
| 252 |
]); |
| 253 |
|
| 254 |
|
| 255 |
if ($order->subscriptions && $order->subscriptions->count() > 0) { |
| 256 |
\FluentCart\App\App::make('view')->render('invoice.parts.subscription_items', [ |
| 257 |
'subscriptions' => $order->subscriptions, |
| 258 |
'order' => $order |
| 259 |
]); |
| 260 |
} |
| 261 |
|
| 262 |
$licenses = $order->getLicenses(); |
| 263 |
if ($licenses && $licenses->count() > 0) { |
| 264 |
\FluentCart\App\App::make('view')->render('emails.parts.licenses', [ |
| 265 |
'licenses' => $licenses, |
| 266 |
'heading' => __('Licenses', 'fluent-cart'), |
| 267 |
'show_notice' => false |
| 268 |
]); |
| 269 |
} |
| 270 |
|
| 271 |
$downloads = $order->getDownloads(); |
| 272 |
if ($downloads) { |
| 273 |
\FluentCart\App\App::make('view')->render('emails.parts.downloads', [ |
| 274 |
'order' => $order, |
| 275 |
'heading' => __('Downloads', 'fluent-cart'), |
| 276 |
'downloadItems' => $downloads, |
| 277 |
]); |
| 278 |
} |
| 279 |
|
| 280 |
echo '<hr />'; |
| 281 |
|
| 282 |
\FluentCart\App\App::make('view')->render('emails.parts.addresses', [ |
| 283 |
'order' => $order, |
| 284 |
]); |
| 285 |
|
| 286 |
return ob_get_clean(); |
| 287 |
|
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
public function getDiscountTotal(): string |
| 292 |
{ |
| 293 |
return (string) ($this->getDiscountTotalInCents() / 100); |
| 294 |
} |
| 295 |
|
| 296 |
public function getDiscountTotalFormatted(): string |
| 297 |
{ |
| 298 |
return CurrencySettings::getPriceHtml($this->getDiscountTotalInCents(), $this->order['currency']); |
| 299 |
} |
| 300 |
|
| 301 |
private function getDiscountTotalInCents(): int |
| 302 |
{ |
| 303 |
return (int) Arr::get($this->order, 'coupon_discount_total', 0) |
| 304 |
+ (int) Arr::get($this->order, 'manual_discount_total', 0); |
| 305 |
} |
| 306 |
|
| 307 |
public function getOrderRef(): string |
| 308 |
{ |
| 309 |
$invoiceNo = Arr::get($this->order, 'invoice_no'); |
| 310 |
|
| 311 |
if (!empty($invoiceNo)) { |
| 312 |
return (string) $invoiceNo; |
| 313 |
} |
| 314 |
|
| 315 |
return (string) Arr::get($this->order, 'id'); |
| 316 |
} |
| 317 |
|
| 318 |
public function getCustomerDashboardAnchorLink($accessor, $code = null, $conditions = []) |
| 319 |
{ |
| 320 |
$defaultValue = Arr::get($conditions, 'default_value') ?? Arr::get($this->order, 'invoice_no'); |
| 321 |
if (empty($this->order)) { |
| 322 |
return $code; |
| 323 |
} |
| 324 |
|
| 325 |
$profilePage = $this->storeSettings->getCustomerProfilePage(); |
| 326 |
|
| 327 |
|
| 328 |
if (!empty($profilePage)) { |
| 329 |
return "<a style='color: #017EF3; text-decoration: none;' href='" . "$profilePage#/order/" . Arr::get($this->order, 'uuid') . "'>" . $defaultValue . "</a>"; |
| 330 |
} else { |
| 331 |
return Arr::get($this->order, 'invoice_no'); |
| 332 |
} |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
public function getCustomerDashboardLink($accessor, $code = null) |
| 337 |
{ |
| 338 |
if (empty($this->order)) { |
| 339 |
return $code; |
| 340 |
} |
| 341 |
|
| 342 |
$orderLink = TemplateService::getCustomerProfileUrl('order/' . Arr::get($this->order, 'uuid')); |
| 343 |
|
| 344 |
return is_user_logged_in() ? $orderLink : wp_login_url($orderLink); |
| 345 |
} |
| 346 |
|
| 347 |
public function getAdminOrderLink($accessor, $code = null) |
| 348 |
{ |
| 349 |
if (empty($this->order)) { |
| 350 |
return $code; |
| 351 |
} |
| 352 |
return admin_url('admin.php?page=fluent-cart#/orders/' . Arr::get($this->order, 'id') . '/view'); |
| 353 |
} |
| 354 |
|
| 355 |
public function getAdminOrderAnchorLink($accessor, $code = null, $conditions = []) |
| 356 |
{ |
| 357 |
$defaultValue = Arr::get($conditions, 'default_value'); |
| 358 |
if (empty($this->order)) { |
| 359 |
return $code; |
| 360 |
} |
| 361 |
|
| 362 |
$url = admin_url('admin.php?page=fluent-cart#/orders/' . Arr::get($this->order, 'id') . '/view'); |
| 363 |
|
| 364 |
if (!empty($defaultValue)) { |
| 365 |
return "<a style='color: #017EF3; text-decoration: none;' href='" . $url . "'>" . $defaultValue . "</a>"; |
| 366 |
} |
| 367 |
|
| 368 |
return $url; |
| 369 |
} |
| 370 |
|
| 371 |
public function getCustomerOrderLink($accessor, $code = null) |
| 372 |
{ |
| 373 |
if (empty($this->order)) { |
| 374 |
return $code; |
| 375 |
} |
| 376 |
|
| 377 |
$customerProfilePage = $this->storeSettings->getCustomerProfilePage(); |
| 378 |
$orderLink = $customerProfilePage . '#/order/' . Arr::get($this->order, 'uuid'); |
| 379 |
|
| 380 |
return is_user_logged_in() ? $orderLink : wp_login_url($orderLink); |
| 381 |
} |
| 382 |
|
| 383 |
public function getTotalAmount() |
| 384 |
{ |
| 385 |
$total = ($this->order['total_amount'] / 100); |
| 386 |
$currency_sign = $this->order['currency']; |
| 387 |
return $total . $currency_sign; |
| 388 |
} |
| 389 |
|
| 390 |
public function getDownloads() |
| 391 |
{ |
| 392 |
$order = $this->order; |
| 393 |
|
| 394 |
$downloads = $order->getDownloads(); |
| 395 |
if ($downloads) { |
| 396 |
return (string)\FluentCart\App\App::make('view')->make('emails.parts.downloads', [ |
| 397 |
'order' => $order, |
| 398 |
'heading' => '', |
| 399 |
'downloadItems' => $downloads |
| 400 |
]); |
| 401 |
} |
| 402 |
|
| 403 |
return ''; |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
public function getLicenses() |
| 408 |
{ |
| 409 |
$order = $this->order; |
| 410 |
$licenses = $order->getLicenses(); |
| 411 |
if ($licenses && $licenses->count() > 0) { |
| 412 |
return (string)\FluentCart\App\App::make('view')->make('emails.parts.licenses', [ |
| 413 |
'licenses' => $licenses, |
| 414 |
'heading' => __('Licenses', 'fluent-cart'), |
| 415 |
'show_notice' => false |
| 416 |
]); |
| 417 |
} |
| 418 |
|
| 419 |
return ''; |
| 420 |
} |
| 421 |
|
| 422 |
public function getLicenseCount(): string |
| 423 |
{ |
| 424 |
return (string)$this->licenses->count(); |
| 425 |
} |
| 426 |
|
| 427 |
public function getIsDigital(): string |
| 428 |
{ |
| 429 |
if (!$this->order) { |
| 430 |
return 'no'; |
| 431 |
} |
| 432 |
|
| 433 |
$fulfillmentType = Arr::get($this->order, 'fulfillment_type'); |
| 434 |
|
| 435 |
return $fulfillmentType === 'digital' ? 'yes' : 'no'; |
| 436 |
} |
| 437 |
|
| 438 |
public function getItemCount(): string |
| 439 |
{ |
| 440 |
$orderItems = $this->order ? $this->order->order_items : null; |
| 441 |
|
| 442 |
if ($orderItems) { |
| 443 |
return (string)$orderItems->count(); |
| 444 |
} |
| 445 |
|
| 446 |
return '0'; |
| 447 |
} |
| 448 |
|
| 449 |
public function getSubscriptions() |
| 450 |
{ |
| 451 |
return ''; |
| 452 |
} |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|