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 / Handlers / SimpleProductHandler.php

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

164 lines 5.2 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\Handlers;
4
5 use SyncBasalam\Admin\Product\Data\Services\CategoryService;
6 use SyncBasalam\Admin\Product\Data\Services\PriceService;
7 use SyncBasalam\Admin\Product\Data\Services\PhotoService;
8 use SyncBasalam\Admin\Product\Data\Services\VideoService;
9 use SyncBasalam\Admin\Product\Data\Services\AttributeService;
10 use SyncBasalam\Admin\Settings\SettingsConfig;
11 use SyncBasalam\Services\Products\PreparationDaysGuard;
12
13 defined('ABSPATH') || exit;
14
15 class SimpleProductHandler implements ProductDataHandlerInterface
16 {
17 private $categoryService;
18 private $priceService;
19 private $photoService;
20 private $videoService;
21 private $attributeService;
22 private $preparationGuard;
23 private array $settings;
24
25 public function __construct()
26 {
27 $this->categoryService = new CategoryService();
28 $this->priceService = new PriceService();
29 $this->photoService = new PhotoService();
30 $this->videoService = new VideoService();
31 $this->attributeService = new AttributeService();
32 $this->preparationGuard = new PreparationDaysGuard();
33 $this->settings = syncBasalamSettings()->getSettings();
34 }
35
36 public function getName($product): string
37 {
38 $baseName = $product->get_name();
39
40 $prefix = $this->settings[SettingsConfig::PRODUCT_PREFIX_TITLE];
41 $suffix = $this->settings[SettingsConfig::PRODUCT_SUFFIX_TITLE];
42
43 $name = $prefix ? "{$prefix} {$baseName}" : $baseName;
44 $name = $suffix ? "{$name} {$suffix}" : $name;
45
46 $attributeSuffix = $this->attributeService->getAttributeSuffix($product);
47 if ($attributeSuffix) $name .= " ({$attributeSuffix})";
48
49 return mb_substr($name, 0, 120);
50 }
51
52 public function getSku($product): ?string
53 {
54 $sku = trim((string) $product->get_sku());
55
56 return $sku !== '' ? $sku : null;
57 }
58
59 public function getDescription($product): string
60 {
61 return $this->attributeService->generateDescription($product);
62 }
63
64 public function getCategoryId($product): ?int
65 {
66 return $this->categoryService->getPrimaryCategoryId($product);
67 }
68
69 public function getCategoryIds($product): array
70 {
71 return $this->categoryService->getCategoryIds($product);
72 }
73
74 public function getPrice($product): ?int
75 {
76 return $this->priceService->calculateFinalPrice($product);
77 }
78
79 public function getStock($product): int
80 {
81 $defaultStock = $this->settings[SettingsConfig::DEFAULT_STOCK_QUANTITY];
82 $safeStock = $this->settings[SettingsConfig::SAFE_STOCK];
83 $stock = $product->get_stock_quantity();
84 $stockStatus = $product->get_stock_status();
85
86 $calculatedStock = $stockStatus === 'instock' ? $stock ?? $defaultStock : 0;
87
88 if ($safeStock > 0 && $calculatedStock <= $safeStock) return 0;
89
90 return $calculatedStock;
91 }
92
93 public function getWeight($product): ?int
94 {
95 if (empty($product->get_weight())) return $this->settings[SettingsConfig::DEFAULT_WEIGHT];
96
97 $weight = str_replace(',', '.', $product->get_weight());
98 $weightUnit = get_option('woocommerce_weight_unit');
99
100 return ($weightUnit === 'kg') ? floatval($weight) * 1000 : floatval($weight);
101 }
102
103 public function getPackageWeight($product): int
104 {
105 $weight = $this->getWeight($product) ?? 0;
106 $packageWeight = $this->settings[SettingsConfig::DEFAULT_PACKAGE_WEIGHT];
107
108 return intval($weight + $packageWeight);
109 }
110
111 public function getMainPhoto($product): ?int
112 {
113 return $this->photoService->getMainPhotoId($product);
114 }
115
116 public function getGalleryPhotos($product): array
117 {
118 return $this->photoService->getGalleryPhotoIds($product);
119 }
120
121 public function getVideo($product): ?int
122 {
123 return $this->videoService->getVideoFileId($product);
124 }
125
126 public function getPreparationDays($product): int
127 {
128 $preparationDays = (int) $this->settings[SettingsConfig::DEFAULT_PREPARATION];
129
130 return $this->preparationGuard->enforce($preparationDays, $product);
131 }
132
133 public function getUnitType($product): int
134 {
135 $unitType = get_post_meta($product->get_id(), '_sync_basalam_product_unit', true);
136 return ($unitType && is_numeric($unitType)) ? intval($unitType) : 6304;
137 }
138
139 public function getUnitQuantity($product): int
140 {
141 $quantity = get_post_meta($product->get_id(), '_sync_basalam_product_value', true);
142 return is_numeric($quantity) ? intval($quantity) : 1;
143 }
144
145 public function isWholesale($product): bool
146 {
147 $globalSetting = $this->settings[SettingsConfig::ALL_PRODUCTS_WHOLESALE];
148
149 if ($globalSetting === 'all') return true;
150
151 return get_post_meta($product->get_id(), '_sync_basalam_is_wholesale', true) === 'yes';
152 }
153
154 public function getVariants($product): array
155 {
156 return [];
157 }
158
159 public function getAttributes($product): array
160 {
161 return $this->attributeService->getBasalamAttributes($product);
162 }
163 }
164