| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Operations; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Operations\AbstractProductOperation; |
| 6 |
use SyncBasalam\Services\Products\CreateSingleProductService; |
| 7 |
use SyncBasalam\Services\Products\Discount\DiscountTaskProcessor; |
| 8 |
use SyncBasalam\Logger\Logger; |
| 9 |
use SyncBasalam\Admin\Product\Data\ProductDataFacade; |
| 10 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 11 |
|
| 12 |
defined('ABSPATH') || exit; |
| 13 |
|
| 14 |
class CreateProduct extends AbstractProductOperation |
| 15 |
{ |
| 16 |
private $createProductService; |
| 17 |
private $discountProcessor; |
| 18 |
|
| 19 |
public function __construct( |
| 20 |
$createProductService = null, |
| 21 |
$discountProcessor = null |
| 22 |
) |
| 23 |
{ |
| 24 |
parent::__construct(); |
| 25 |
$this->createProductService = $createProductService ?: syncBasalamContainer()->get(CreateSingleProductService::class); |
| 26 |
$this->discountProcessor = $discountProcessor ?: syncBasalamContainer()->get(DiscountTaskProcessor::class); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
protected function run(int $product_id, array $args = []): array |
| 31 |
{ |
| 32 |
$categoryIds = $args['category_ids'] ?? []; |
| 33 |
|
| 34 |
$productData = ProductDataFacade::get($product_id, $categoryIds); |
| 35 |
|
| 36 |
$createResult = $this->createProductService->createProductInBasalam($productData, $product_id); |
| 37 |
|
| 38 |
if ($createResult['success']) $this->discountProcessor->handleProductDiscount($product_id); |
| 39 |
|
| 40 |
return $createResult; |
| 41 |
} |
| 42 |
|
| 43 |
public function validate(int $product_id): bool |
| 44 |
{ |
| 45 |
if (!parent::validate($product_id)) return false; |
| 46 |
|
| 47 |
$basalamProductId = get_post_meta($product_id, ProductMetaKey::basalamProductId(), true); |
| 48 |
if (!empty($basalamProductId)) { |
| 49 |
Logger::warning(sprintf('� |
| 50 |
حصول %d از قبل به باسلا� |
| 51 |
� |
| 52 |
تصل است.', $product_id), [ |
| 53 |
'product_id' => $product_id, |
| 54 |
'شناسه_� |
| 55 |
حصول_باسلا� |
| 56 |
' => $basalamProductId, |
| 57 |
'ع� |
| 58 |
لیات' => $this->getOperationNameFromClass() |
| 59 |
]); |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
} |
| 66 |
|