PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.8.8
ووسلام – همگام سازی ووکامرس و باسلام v1.8.8
1.10.21 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 All 54 releases
sync-basalam / includes / Admin / Product / Data / Handlers / SimpleProductHandler.php

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

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