| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ProductShippingData |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class ProductShippingData |
| 12 |
{ |
| 13 |
|
| 14 |
public const FIELD_SHIPPING_INFO = 'shipping_info'; |
| 15 |
public const FIELD_COUNTRY_TO = 'country_to'; |
| 16 |
public const FIELD_COUNTRY_FROM = 'country_from'; |
| 17 |
public const FIELD_METHOD = 'method'; |
| 18 |
public const FIELD_COST = 'cost'; |
| 19 |
public const FIELD_VARIATION_KEY = 'variation_key'; |
| 20 |
|
| 21 |
private array $shippingInfo = []; |
| 22 |
private ?string $countryFrom = null; |
| 23 |
private ?string $countryTo = null; |
| 24 |
private ?string $method = null; |
| 25 |
private ?string $cost = null; |
| 26 |
private ?string $variationKey = null; |
| 27 |
|
| 28 |
|
| 29 |
public function getShippingInfo(): array |
| 30 |
{ |
| 31 |
return $this->shippingInfo; |
| 32 |
} |
| 33 |
|
| 34 |
public function setShippingInfo(array $shippingInfo): self |
| 35 |
{ |
| 36 |
$this->shippingInfo = $shippingInfo; |
| 37 |
|
| 38 |
return $this; |
| 39 |
} |
| 40 |
|
| 41 |
public function getCountryFrom(): ?string |
| 42 |
{ |
| 43 |
return $this->countryFrom; |
| 44 |
} |
| 45 |
|
| 46 |
public function setCountryFrom(?string $countryFrom): self |
| 47 |
{ |
| 48 |
$this->countryFrom = $countryFrom; |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
public function getCountryTo(): ?string |
| 54 |
{ |
| 55 |
return $this->countryTo; |
| 56 |
} |
| 57 |
|
| 58 |
public function setCountryTo(?string $countryTo): self |
| 59 |
{ |
| 60 |
$this->countryTo = $countryTo; |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function getMethod(): ?string |
| 66 |
{ |
| 67 |
return $this->method; |
| 68 |
} |
| 69 |
|
| 70 |
public function setMethod(?string $method): self |
| 71 |
{ |
| 72 |
$this->method = $method; |
| 73 |
|
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
public function getCost(): ?string |
| 78 |
{ |
| 79 |
return $this->cost; |
| 80 |
} |
| 81 |
|
| 82 |
public function setCost(?string $cost): self |
| 83 |
{ |
| 84 |
$this->cost = $cost; |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
public function getVariationKey(): ?string |
| 90 |
{ |
| 91 |
return $this->variationKey; |
| 92 |
} |
| 93 |
|
| 94 |
public function setVariationKey(?string $variationKey): self |
| 95 |
{ |
| 96 |
$this->variationKey = $variationKey; |
| 97 |
|
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
public function getShippingByQuantity(int $quantity): array |
| 102 |
{ |
| 103 |
if (empty($this->shippingInfo)) { |
| 104 |
return []; |
| 105 |
} |
| 106 |
|
| 107 |
$shippingInfoByQuantity = []; |
| 108 |
|
| 109 |
foreach ($this->shippingInfo as $countryCode => $shippingInfo) { |
| 110 |
if (!empty($this->shippingInfo[$countryCode][$quantity])) { |
| 111 |
$shippingInfoByQuantity[$countryCode] = $this->shippingInfo[$countryCode][$quantity]; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $shippingInfoByQuantity; |
| 116 |
} |
| 117 |
|
| 118 |
public function getItems(int $quantity, string $fromCountry, string $toCountry): ?array |
| 119 |
{ |
| 120 |
//for now we save and get shipping for quantity 1 only. |
| 121 |
$quantity = 1; |
| 122 |
$countryCode = self::meta_key($fromCountry, $toCountry); |
| 123 |
|
| 124 |
if (isset($this->shippingInfo[$countryCode][$quantity])) { |
| 125 |
return $this->shippingInfo[$countryCode][$quantity]; |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
public function setItems(int $quantity, ?string $fromCountry, string $toCountry, array $items): self |
| 132 |
{ |
| 133 |
$meta_key = self::meta_key($fromCountry, $toCountry); |
| 134 |
|
| 135 |
$this->shippingInfo[$meta_key][$quantity] = $items; |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
|
| 140 |
public function resetDefaultShipping(): self |
| 141 |
{ |
| 142 |
$this->setCost(null) |
| 143 |
->setCountryTo(null) |
| 144 |
->setMethod(null) |
| 145 |
->setCountryTo(null) |
| 146 |
->setVariationKey(null); |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
public function resetShippingInfo(): self |
| 152 |
{ |
| 153 |
$this->shippingInfo = []; |
| 154 |
|
| 155 |
return $this; |
| 156 |
} |
| 157 |
|
| 158 |
public function toArray(): array |
| 159 |
{ |
| 160 |
return [ |
| 161 |
self::FIELD_SHIPPING_INFO => $this->getShippingInfo(), |
| 162 |
self::FIELD_COUNTRY_TO => $this->getCountryTo(), |
| 163 |
self::FIELD_COUNTRY_FROM => $this->getCountryFrom(), |
| 164 |
self::FIELD_METHOD => $this->getMethod(), |
| 165 |
self::FIELD_COST => $this->getCost(), |
| 166 |
self::FIELD_VARIATION_KEY => $this->getVariationKey(), |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @todo: move this code somewhere |
| 172 |
*/ |
| 173 |
public static function meta_key(string $from_country, string $to_country): string |
| 174 |
{ |
| 175 |
|
| 176 |
return ProductShippingData::normalize_country($from_country) . |
| 177 |
ProductShippingData::normalize_country($to_country); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @todo: use AliexpressHelper->convertToAliexpressCountryCode() instead |
| 182 |
* Convert WooCommerce country code to AliExpress country code |
| 183 |
*/ |
| 184 |
public static function normalize_country(?string $country): ?string |
| 185 |
{ |
| 186 |
switch ($country) { |
| 187 |
case 'AQ': |
| 188 |
case 'BV': |
| 189 |
case 'IO': |
| 190 |
case 'CU': |
| 191 |
case 'TF': |
| 192 |
case 'HM': |
| 193 |
case 'IR': |
| 194 |
case 'IM': |
| 195 |
case 'SH': |
| 196 |
case 'PN': |
| 197 |
case 'SD': |
| 198 |
case 'SJ': |
| 199 |
case 'SY': |
| 200 |
case 'TK': |
| 201 |
case 'UM': |
| 202 |
case 'EH': |
| 203 |
$country = 'OTHER'; |
| 204 |
break; |
| 205 |
case 'AX': |
| 206 |
$country = 'ALA'; |
| 207 |
break; |
| 208 |
// case 'CN': |
| 209 |
// $country = 'HK'; |
| 210 |
break; |
| 211 |
case 'CD': |
| 212 |
$country = 'ZR'; |
| 213 |
break; |
| 214 |
case 'GG': |
| 215 |
$country = 'GGY'; |
| 216 |
break; |
| 217 |
case 'JE': |
| 218 |
$country = 'JEY'; |
| 219 |
break; |
| 220 |
case 'ME': |
| 221 |
$country = 'MNE'; |
| 222 |
break; |
| 223 |
case 'KP': |
| 224 |
$country = 'KR'; |
| 225 |
break; |
| 226 |
case 'BL': |
| 227 |
$country = 'BLM'; |
| 228 |
break; |
| 229 |
case 'MF': |
| 230 |
$country = 'MAF'; |
| 231 |
break; |
| 232 |
case 'RS': |
| 233 |
$country = 'SRB'; |
| 234 |
break; |
| 235 |
case 'GS': |
| 236 |
$country = 'SGS'; |
| 237 |
break; |
| 238 |
case 'TL': |
| 239 |
$country = 'TLS'; |
| 240 |
break; |
| 241 |
case 'GB': |
| 242 |
$country = 'UK'; |
| 243 |
break; |
| 244 |
default: |
| 245 |
} |
| 246 |
|
| 247 |
$country = trim($country); |
| 248 |
if (empty($country)) { |
| 249 |
return 'CN'; |
| 250 |
} |
| 251 |
|
| 252 |
return $country; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|