| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Report\Concerns; |
| 4 |
|
| 5 |
use FluentCart\App\Services\Report\ReportService; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
use FluentCart\Framework\Support\DateTime; |
| 8 |
|
| 9 |
trait CanParseAddressField |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* Parse Address value from given data |
| 14 |
* |
| 15 |
* @param mixed $item |
| 16 |
* @param string $groupKey The name of the group |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
protected function parseAddressField($item, string $groupKey): string |
| 20 |
{ |
| 21 |
$allowedKeys = [ |
| 22 |
'shipping_country', |
| 23 |
'billing_country', |
| 24 |
'shipping_city', |
| 25 |
'billing_city', |
| 26 |
'shipping_state', |
| 27 |
'billing_state' |
| 28 |
]; |
| 29 |
|
| 30 |
if (!in_array($groupKey, $allowedKeys)) { |
| 31 |
return __('Unknown', 'fluent-cart'); |
| 32 |
} |
| 33 |
|
| 34 |
list($addressType, $field) = explode('_', $groupKey); |
| 35 |
// Check if order_addresses exist |
| 36 |
if (Arr::exists($item, 'order_addresses')) { |
| 37 |
foreach ($item['order_addresses'] as $address) { |
| 38 |
// Check if the address type matches (billing or shipping) |
| 39 |
if (Arr::get($address, 'type') === $addressType) { |
| 40 |
// Return the requested field if it exists, otherwise return 'Unknown' |
| 41 |
return Arr::get($address, $field, __('Unknown', 'fluent-cart')); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return __('Unknown', 'fluent-cart'); // Fallback if no matching address or field found |
| 47 |
} |
| 48 |
} |