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

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

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