| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Config\Endpoints; |
| 6 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 7 |
use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 8 |
use SyncBasalam\Logger\Logger; |
| 9 |
use SyncBasalam\Services\ApiServiceManager; |
| 10 |
|
| 11 |
defined('ABSPATH') || exit; |
| 12 |
|
| 13 |
class UpdateProductVariationsService |
| 14 |
{ |
| 15 |
/** Variant fields that are sent to the single-variation endpoint. */ |
| 16 |
private const VARIATION_FIELDS = ['primary_price', 'stock']; |
| 17 |
|
| 18 |
private $apiservice; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->apiservice = syncBasalamContainer()->get(ApiServiceManager::class); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* True when every variant already has a Basalam variation id, so each one can be |
| 27 |
* updated with its own request instead of being sent inside the product payload. |
| 28 |
*/ |
| 29 |
public static function allVariantsHaveBasalamId(array $variants): bool |
| 30 |
{ |
| 31 |
if (empty($variants)) return false; |
| 32 |
|
| 33 |
foreach ($variants as $variant) { |
| 34 |
if (empty($variant['id'])) return false; |
| 35 |
} |
| 36 |
|
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Sends one PATCH request per variation. |
| 42 |
* |
| 43 |
* @return array{updated: int, skipped: int, failed: int} |
| 44 |
*/ |
| 45 |
public function updateVariations($basalamProductId, array $variants, $productId = null): array |
| 46 |
{ |
| 47 |
$updated = 0; |
| 48 |
$skipped = 0; |
| 49 |
$failed = 0; |
| 50 |
$firstError = null; |
| 51 |
|
| 52 |
foreach ($variants as $variant) { |
| 53 |
$data = array_intersect_key($variant, array_flip(self::VARIATION_FIELDS)); |
| 54 |
$data = array_filter($data, fn($value) => $value !== null); |
| 55 |
|
| 56 |
if (empty($data)) { |
| 57 |
$skipped++; |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$url = sprintf(Endpoints::PRODUCT_VARIATION_UPDATE, $basalamProductId, $variant['id']); |
| 62 |
|
| 63 |
try { |
| 64 |
$this->apiservice->patch($url, $data); |
| 65 |
$updated++; |
| 66 |
} catch (RetryableException $e) { |
| 67 |
throw $e; |
| 68 |
} catch (\Exception $e) { |
| 69 |
$failed++; |
| 70 |
$firstError = $firstError ?: $e->getMessage(); |
| 71 |
|
| 72 |
Logger::error('خطا در بروزرسانی � |
| 73 |
تغیر � |
| 74 |
حصول در باسلا� |
| 75 |
: ' . $e->getMessage(), [ |
| 76 |
'operation' => 'بروزرسانی � |
| 77 |
تغیر � |
| 78 |
حصول', |
| 79 |
'product_id' => $productId, |
| 80 |
'basalam_product_id' => $basalamProductId, |
| 81 |
'basalam_variation_id' => $variant['id'], |
| 82 |
]); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Every variation failed with a permanent error: report it instead of pretending success. |
| 87 |
if ($updated === 0 && $failed > 0) { |
| 88 |
throw NonRetryableException::permanent(esc_html('بروزرسانی � |
| 89 |
تغیرهای � |
| 90 |
حصول نا� |
| 91 |
وفق بود: ' . $firstError)); |
| 92 |
} |
| 93 |
|
| 94 |
return ['updated' => $updated, 'skipped' => $skipped, 'failed' => $failed]; |
| 95 |
} |
| 96 |
} |
| 97 |
|