PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.8
ووسلام – همگام سازی ووکامرس و باسلام v1.10.8
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.8, at includes/Admin/Product/Data/Strategies/CustomUpdateProductStrategy.php

112 lines 4.9 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('photo', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
32 $data['photo'] = $handler->getMainPhoto($product);
33 }
34 if (!array_key_exists('photos', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
35 $data['photos'] = $handler->getGalleryPhotos($product);
36 }
37
38 if (!array_key_exists('video', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VIDEO)) {
39 $video = $handler->getVideo($product);
40 if ($video !== null) {
41 $data['video'] = $video;
42 }
43 }
44
45 if (!array_key_exists('primary_price', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) {
46 if (!$product->is_type('variable')) {
47 $data['primary_price'] = $handler->getPrice($product);
48 }
49 }
50
51 if (!array_key_exists('stock', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) {
52 if (!$product->is_type('variable')) {
53 $data['stock'] = $handler->getStock($product);
54 }
55 }
56
57 if (!array_key_exists('variants', $data) && $product->is_type('variable')) {
58 $variants = $this->collectVariants($product, $handler);
59 if (!empty($variants)) $data['variants'] = $variants;
60 }
61
62 if (!array_key_exists('weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
63 $data['weight'] = $handler->getWeight($product);
64 }
65 if (!array_key_exists('package_weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
66 $data['package_weight'] = $handler->getPackageWeight($product);
67 }
68
69 if (!array_key_exists('description', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_DESCRIPTION)) {
70 $data['description'] = $handler->getDescription($product);
71 }
72
73 if (!array_key_exists('product_attribute', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_ATTR)) {
74 $data['product_attribute'] = $handler->getAttributes($product);
75 }
76
77 return array_filter($data, fn($value) => $value !== null);
78 }
79
80 private function collectVariants($product, ProductDataHandlerInterface $handler): array
81 {
82 $variants = $handler->getVariants($product);
83 if (empty($variants)) return [];
84
85 // When at least one variation is not connected to Basalam yet, the product is updated with a
86 // single request and the variants section must be complete (properties, price and stock) so
87 // Basalam can match the variations and their ids can be stored.
88 if (!UpdateProductVariationsService::allVariantsHaveBasalamId($variants)) return $variants;
89
90 $syncPrice = $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VARIANT_PRICE);
91 $syncStock = $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VARIANT_STOCK);
92
93 if (!$syncPrice && !$syncStock) return [];
94
95 // Connected variations are patched one by one, so only the ticked fields are sent.
96 return array_map(function ($variant) use ($syncPrice, $syncStock) {
97 $selected = ['id' => $variant['id']];
98
99 if ($syncPrice && array_key_exists('primary_price', $variant)) $selected['primary_price'] = $variant['primary_price'];
100 if ($syncStock && array_key_exists('stock', $variant)) $selected['stock'] = $variant['stock'];
101
102 return $selected;
103 }, $variants);
104 }
105
106 private function shouldSyncField(string $fieldKey): bool
107 {
108 $setting = syncBasalamSettings()->getSettings($fieldKey);
109 return $setting == true || $setting === '1' || $setting === 1;
110 }
111 }
112