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 / Data / Strategies / CustomUpdateProductStrategy.php

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

121 lines 5.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\Data\Strategies;
4
5 use SyncBasalam\Admin\Product\Data\Handlers\ProductDataHandlerInterface;
6 use SyncBasalam\Admin\Settings\SettingsConfig;
7 use SyncBasalam\Services\Products\UpdateProductVariationsService;
8 use SyncBasalam\Utilities\ProductMetaKey;
9
10 defined('ABSPATH') || exit;
11
12 class CustomUpdateProductStrategy implements DataStrategyInterface
13 {
14 public function collect($product, ProductDataHandlerInterface $handler): array
15 {
16 $data = apply_filters('sync_basalam_product_payload', null, $product, $handler, 'custom_update');
17 if (!is_array($data)) {
18 $data = [];
19 }
20
21 $basalamProductId = get_post_meta($product->get_id(), ProductMetaKey::basalamProductId(), true);
22
23 if (!array_key_exists('id', $data)) {
24 $data['id'] = $basalamProductId;
25 }
26
27 if (!array_key_exists('name', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_NAME)) {
28 $data['name'] = $handler->getName($product);
29 }
30
31 if (!array_key_exists('sku', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_SKU)) {
32 $sku = $handler->getSku($product);
33 if ($sku !== null) {
34 $data['sku'] = $sku;
35 }
36 }
37
38 if (!array_key_exists('photo', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
39 $data['photo'] = $handler->getMainPhoto($product);
40 }
41 if (!array_key_exists('photos', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
42 $data['photos'] = $handler->getGalleryPhotos($product);
43 }
44
45 if (!array_key_exists('video', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VIDEO)) {
46 $video = $handler->getVideo($product);
47 if ($video !== null) {
48 $data['video'] = $video;
49 }
50 }
51
52 if (!array_key_exists('primary_price', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) {
53 if (!$product->is_type('variable')) {
54 $data['primary_price'] = $handler->getPrice($product);
55 }
56 }
57
58 if (!array_key_exists('stock', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) {
59 if (!$product->is_type('variable')) {
60 $data['stock'] = $handler->getStock($product);
61 }
62 }
63
64 if (!array_key_exists('variants', $data) && $product->is_type('variable')) {
65 $variants = $this->collectVariants($product, $handler);
66 if (!empty($variants)) $data['variants'] = $variants;
67 }
68
69 if (!array_key_exists('weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
70 $data['weight'] = $handler->getWeight($product);
71 }
72 if (!array_key_exists('package_weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
73 $data['package_weight'] = $handler->getPackageWeight($product);
74 }
75
76 if (!array_key_exists('description', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_DESCRIPTION)) {
77 $data['description'] = $handler->getDescription($product);
78 }
79
80 if (!array_key_exists('product_attribute', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_ATTR)) {
81 $data['product_attribute'] = $handler->getAttributes($product);
82 }
83
84 return array_filter($data, fn($value) => $value !== null);
85 }
86
87 private function collectVariants($product, ProductDataHandlerInterface $handler): array
88 {
89 $variants = $handler->getVariants($product);
90 if (empty($variants)) return [];
91
92 // When at least one variation is not connected to Basalam yet, the product is updated with a
93 // single request and the variants section must be complete (properties, price and stock) so
94 // Basalam can match the variations and their ids can be stored.
95 if (!UpdateProductVariationsService::allVariantsHaveBasalamId($variants)) return $variants;
96
97 $syncPrice = $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VARIANT_PRICE);
98 $syncStock = $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VARIANT_STOCK);
99 $syncSku = $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_SKU);
100
101 if (!$syncPrice && !$syncStock && !$syncSku) return [];
102
103 // Connected variations are patched one by one, so only the ticked fields are sent.
104 return array_map(function ($variant) use ($syncPrice, $syncStock, $syncSku) {
105 $selected = ['id' => $variant['id']];
106
107 if ($syncPrice && array_key_exists('primary_price', $variant)) $selected['primary_price'] = $variant['primary_price'];
108 if ($syncStock && array_key_exists('stock', $variant)) $selected['stock'] = $variant['stock'];
109 if ($syncSku && array_key_exists('sku', $variant)) $selected['sku'] = $variant['sku'];
110
111 return $selected;
112 }, $variants);
113 }
114
115 private function shouldSyncField(string $fieldKey): bool
116 {
117 $setting = syncBasalamSettings()->getSettings($fieldKey);
118 return $setting == true || $setting === '1' || $setting === 1;
119 }
120 }
121