PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.19
ووسلام – همگام سازی ووکامرس و باسلام v1.10.19
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Admin / Product / Operations / CreateProduct.php

CreateProduct.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.19, at includes/Admin/Product/Operations/CreateProduct.php

73 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
12 use SyncBasalam\Services\VendorSyncPolicy;
13
14 defined('ABSPATH') || exit;
15
16 class CreateProduct extends AbstractProductOperation
17 {
18 private $createProductService;
19 private $discountProcessor;
20
21 public function __construct(
22 $createProductService = null,
23 $discountProcessor = null
24 )
25 {
26 parent::__construct();
27 $this->createProductService = $createProductService ?: syncBasalamContainer()->get(CreateSingleProductService::class);
28 $this->discountProcessor = $discountProcessor ?: syncBasalamContainer()->get(DiscountTaskProcessor::class);
29 }
30
31
32 protected function run(int $product_id, array $args = []): array
33 {
34 $vendorSyncPolicy = syncBasalamContainer()->get(VendorSyncPolicy::class);
35 if (!$vendorSyncPolicy->canCreate()) {
36 throw NonRetryableException::invalidData($vendorSyncPolicy->getRestrictionMessage(false));
37 }
38
39 $categoryIds = $args['category_ids'] ?? [];
40
41 $productData = ProductDataFacade::get($product_id, $categoryIds);
42
43 $createResult = $this->createProductService->createProductInBasalam($productData, $product_id);
44
45 if ($createResult['success']) $this->discountProcessor->handleProductDiscount($product_id);
46
47 return $createResult;
48 }
49
50 public function validate(int $product_id): bool
51 {
52 if (!parent::validate($product_id)) return false;
53
54 $basalamProductId = get_post_meta($product_id, ProductMetaKey::basalamProductId(), true);
55 if (!empty($basalamProductId)) {
56 Logger::warning(sprintf('�
57 حصول %d از قبل به باسلا�
58
59 تصل است.', $product_id), [
60 'product_id' => $product_id,
61 'شناسه_�
62 حصول_باسلا�
63 ' => $basalamProductId,
64 'ع�
65 لیات' => $this->getOperationNameFromClass()
66 ]);
67 return false;
68 }
69
70 return true;
71 }
72 }
73