| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Utilities; |
| 4 |
|
| 5 |
class GetProvincesData |
| 6 |
{ |
| 7 |
public static array $provinces = [ |
| 8 |
'KHZ' => 'خوزستان', |
| 9 |
'THR' => 'تهران', |
| 10 |
'ILM' => 'ایلا� |
| 11 |
', |
| 12 |
'BHR' => 'بوشهر', |
| 13 |
'ADL' => 'اردبیل', |
| 14 |
'ESF' => 'اصفهان', |
| 15 |
'YZD' => 'یزد', |
| 16 |
'KRH' => 'کر� |
| 17 |
انشاه', |
| 18 |
'KRN' => 'کر� |
| 19 |
ان', |
| 20 |
'HDN' => 'ه� |
| 21 |
دان', |
| 22 |
'GZN' => 'قزوین', |
| 23 |
'ZJN' => 'زنجان', |
| 24 |
'LRS' => 'لرستان', |
| 25 |
'ABZ' => 'البرز', |
| 26 |
'EAZ' => 'آذربایجان شرقی', |
| 27 |
'WAZ' => 'آذربایجان غربی', |
| 28 |
'CHB' => 'چهار� |
| 29 |
حال و بختیاری', |
| 30 |
'SKH' => 'خراسان جنوبی', |
| 31 |
'RKH' => 'خراسان رضوی', |
| 32 |
'NKH' => 'خراسان ش� |
| 33 |
الی', |
| 34 |
'SMN' => 'س� |
| 35 |
نان', |
| 36 |
'FRS' => 'فارس', |
| 37 |
'QHM' => 'ق� |
| 38 |
', |
| 39 |
'KRD' => 'کردستان', |
| 40 |
'KBD' => 'کهگیلویه و بویراح� |
| 41 |
د', |
| 42 |
'GLS' => 'گلستان', |
| 43 |
'GIL' => 'گیلان', |
| 44 |
'MZN' => '� |
| 45 |
ازندران', |
| 46 |
'MKZ' => '� |
| 47 |
رکزی', |
| 48 |
'HRZ' => 'هر� |
| 49 |
زگان', |
| 50 |
'SBN' => 'سیستان و بلوچستان', |
| 51 |
]; |
| 52 |
|
| 53 |
public static function getCodeByName(string $persianName): ?string |
| 54 |
{ |
| 55 |
foreach (self::$provinces as $code => $name) { |
| 56 |
if (trim($name) === trim($persianName)) return $code; |
| 57 |
} |
| 58 |
|
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
public static function getNameByCode(string $code): ?string |
| 63 |
{ |
| 64 |
return self::$provinces[$code] ?? null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Check if Persian WooCommerce Shipping plugin is active. |
| 69 |
*/ |
| 70 |
public static function isPWSActive(): bool |
| 71 |
{ |
| 72 |
return in_array( |
| 73 |
'persian-woocommerce-shipping/woocommerce-shipping.php', |
| 74 |
(array) get_option('active_plugins', []) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Set order address with PWS compatibility |
| 80 |
* If PWS is active, sets Tapin state/city IDs as meta data |
| 81 |
* Otherwise, uses normal WooCommerce state/city fields. |
| 82 |
*/ |
| 83 |
public static function setOrderAddress($order, $addressData, $type = 'billing') |
| 84 |
{ |
| 85 |
if (!$order || !isset($addressData['province']) || !isset($addressData['city'])) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$province = trim($addressData['province']); |
| 90 |
$city = trim($addressData['city']); |
| 91 |
|
| 92 |
if (self::isPWSActive()) { |
| 93 |
// PWS is active - set Tapin IDs |
| 94 |
$state_id = self::getTapinStateIdByName($province); |
| 95 |
$city_id = self::getTapinCityIdByName($city, $state_id); |
| 96 |
|
| 97 |
if ($state_id) { |
| 98 |
$order->update_meta_data("_{$type}_state_id", $state_id); |
| 99 |
// Also set the state name for WooCommerce |
| 100 |
if ($type === 'billing') { |
| 101 |
$order->set_billing_state($province); |
| 102 |
} else { |
| 103 |
$order->set_shipping_state($province); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ($city_id) { |
| 108 |
$order->update_meta_data("_{$type}_city_id", $city_id); |
| 109 |
// Also set the city name for WooCommerce |
| 110 |
if ($type === 'billing') { |
| 111 |
$order->set_billing_city($city); |
| 112 |
} else { |
| 113 |
$order->set_shipping_city($city); |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
// PWS is not active - use normal WooCommerce fields |
| 118 |
if ($type === 'billing') { |
| 119 |
$order->set_billing_state($province); |
| 120 |
$order->set_billing_city($city); |
| 121 |
} else { |
| 122 |
$order->set_shipping_state($province); |
| 123 |
$order->set_shipping_city($city); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get Tapin state ID by Persian name. |
| 130 |
*/ |
| 131 |
private static function getTapinStateIdByName($stateName): ?int |
| 132 |
{ |
| 133 |
if (!class_exists('\PWS_Tapin')) return null; |
| 134 |
|
| 135 |
$states = call_user_func(['\PWS_Tapin', 'states']); |
| 136 |
if (!$states) return null; |
| 137 |
|
| 138 |
$stateName = trim($stateName); |
| 139 |
foreach ($states as $state_id => $state_title) { |
| 140 |
if (trim($state_title) === $stateName) return (int) $state_id; |
| 141 |
} |
| 142 |
|
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get Tapin city ID by Persian name. |
| 148 |
*/ |
| 149 |
private static function getTapinCityIdByName($cityName, $stateId = null): ?int |
| 150 |
{ |
| 151 |
if (!class_exists('\PWS_Tapin')) return null; |
| 152 |
|
| 153 |
$cities = call_user_func(['\PWS_Tapin', 'cities'], $stateId); |
| 154 |
if (!$cities) return null; |
| 155 |
|
| 156 |
$cityName = trim($cityName); |
| 157 |
foreach ($cities as $city_id => $city_title) { |
| 158 |
if (trim($city_title) === $cityName) return (int) $city_id; |
| 159 |
} |
| 160 |
|
| 161 |
return null; |
| 162 |
} |
| 163 |
} |
| 164 |
|