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

83 lines 3.5 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\Utilities\ProductMetaKey;
8
9 defined('ABSPATH') || exit;
10
11 class CustomUpdateProductStrategy implements DataStrategyInterface
12 {
13 public function collect($product, ProductDataHandlerInterface $handler): array
14 {
15 $data = apply_filters('sync_basalam_product_payload', null, $product, $handler, 'custom_update');
16 if (!is_array($data)) {
17 $data = [];
18 }
19
20 $basalamProductId = get_post_meta($product->get_id(), ProductMetaKey::basalamProductId(), true);
21
22 if (!array_key_exists('id', $data)) {
23 $data['id'] = $basalamProductId;
24 }
25
26 if (!array_key_exists('name', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_NAME)) {
27 $data['name'] = $handler->getName($product);
28 }
29
30 if (!array_key_exists('photo', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
31 $data['photo'] = $handler->getMainPhoto($product);
32 }
33 if (!array_key_exists('photos', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) {
34 $data['photos'] = $handler->getGalleryPhotos($product);
35 }
36
37 if (!array_key_exists('primary_price', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) {
38 if (!$product->is_type('variable')) {
39 $data['primary_price'] = $handler->getPrice($product);
40 }
41 }
42 if (!array_key_exists('variants', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) {
43 $data['variants'] = $handler->getVariants($product);
44 }
45
46 if (!array_key_exists('stock', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) {
47 if (!$product->is_type('variable')) {
48 $data['stock'] = $handler->getStock($product);
49 }
50 }
51 if (!array_key_exists('variants', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) {
52 $data['variants'] = $handler->getVariants($product);
53 }
54
55 if (!array_key_exists('weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
56 $data['weight'] = $handler->getWeight($product);
57 }
58 if (!array_key_exists('package_weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) {
59 $data['package_weight'] = $handler->getPackageWeight($product);
60 }
61
62 if (!array_key_exists('description', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_DESCRIPTION)) {
63 $data['description'] = $handler->getDescription($product);
64 }
65
66 if (!array_key_exists('product_attribute', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_ATTR)) {
67 $data['product_attribute'] = $handler->getAttributes($product);
68 }
69
70 if (!array_key_exists('variants', $data)) {
71 $data['variants'] = [];
72 }
73
74 return array_filter($data, fn($value) => $value !== null);
75 }
76
77 private function shouldSyncField(string $fieldKey): bool
78 {
79 $setting = syncBasalamSettings()->getSettings($fieldKey);
80 return $setting == true || $setting === '1' || $setting === 1;
81 }
82 }
83