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