| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Actions\Controller\ProductActions; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\ProductOperations; |
| 6 |
use SyncBasalam\Actions\Controller\ActionController; |
| 7 |
use SyncBasalam\Logger\Logger; |
| 8 |
use SyncBasalam\Services\VendorSyncPolicy; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class UpdateSingleProduct extends ActionController |
| 13 |
{ |
| 14 |
public function __invoke() |
| 15 |
{ |
| 16 |
$vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class); |
| 17 |
if (!$vendorSyncPolicy->canUpdate()) { |
| 18 |
return wp_send_json_error(['message' => $vendorSyncPolicy->getRestrictionMessage(false)], 409); |
| 19 |
} |
| 20 |
|
| 21 |
$productId = isset($_POST['product_id']) ? sanitize_text_field(wp_unslash($_POST['product_id'])) : null; |
| 22 |
|
| 23 |
$productOperations = syncBasalamContainer()->get(ProductOperations::class); |
| 24 |
|
| 25 |
if (isset($_POST['cat_id'])) { |
| 26 |
$categoryIds = sanitize_text_field(wp_unslash($_POST['cat_id'])); |
| 27 |
|
| 28 |
if (strpos($categoryIds, ',') !== false) { |
| 29 |
$categoryIds = explode(',', $categoryIds); |
| 30 |
} else { |
| 31 |
$categoryIds = [$categoryIds]; |
| 32 |
} |
| 33 |
} else { |
| 34 |
$categoryIds = null; |
| 35 |
} |
| 36 |
|
| 37 |
if (!$productId) { |
| 38 |
wp_send_json_error('آیدی � |
| 39 |
حصول الزا� |
| 40 |
ی است.', 400); |
| 41 |
} |
| 42 |
try { |
| 43 |
$result = $productOperations->updateExistProduct($productId, $categoryIds); |
| 44 |
} catch (\Exception $e) { |
| 45 |
Logger::error("خطا در بروزرسانی � |
| 46 |
حصول: " . $e->getMessage(), [ |
| 47 |
'product_id' => intval($productId), |
| 48 |
'operation' => 'بروزرسانی � |
| 49 |
حصول', |
| 50 |
]); |
| 51 |
wp_send_json_error(['message' => $e->getMessage()], 500); |
| 52 |
} |
| 53 |
|
| 54 |
if (!$result['success']) wp_send_json_error(['message' => $result['message']], $result['status_code'] ?? 500); |
| 55 |
|
| 56 |
wp_send_json_success(['message' => $result['message']], $result['status_code'] ?? 200); |
| 57 |
} |
| 58 |
} |
| 59 |
|