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 / Services / AttributeService.php

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

205 lines 6.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\Services;
4
5 use SyncBasalam\Admin\Settings\SettingsConfig;
6 use SyncBasalam\Utilities\TextConverter;
7 use SyncBasalam\Services\Products\GetCategoryAttr;
8
9 defined('ABSPATH') || exit;
10
11 class AttributeService
12 {
13 private $mobileDataHandler;
14 private $goldDataHandler;
15
16 public function __construct()
17 {
18 $this->mobileDataHandler = new MobileDataHandler();
19 $this->goldDataHandler = new GoldDataHandler();
20 }
21
22 public function getAttributeSuffix($product): ?string
23 {
24 $isEnabled = syncBasalamSettings()->getSettings(SettingsConfig::PRODUCT_ATTRIBUTE_SUFFIX_ENABLED);
25
26 if ($isEnabled !== 'yes') return null;
27
28 $attributeName = trim(syncBasalamSettings()->getSettings(SettingsConfig::PRODUCT_ATTRIBUTE_SUFFIX_PRIORITY));
29
30 if (empty($attributeName)) return null;
31
32 $wooAttributes = $this->getWooCommerceAttributes($product);
33
34 foreach ($wooAttributes as $attribute) {
35 if (trim($attribute['title']) === $attributeName && !empty($attribute['value'])) return $attribute['value'];
36 }
37
38 return null;
39 }
40
41 public function generateDescription($product): string
42 {
43 $descriptionParts = [];
44
45 if (syncBasalamSettings()->getSettings(SettingsConfig::ADD_SHORT_DESC_TO_DESC_PRODUCT) === 'yes') {
46 $shortDesc = TextConverter::convertHtmlToPlainText($product->get_short_description());
47
48 if (!empty($shortDesc)) $descriptionParts[] = trim($shortDesc);
49 }
50
51 $mainDesc = TextConverter::convertHtmlToPlainText($product->get_description());
52
53 if (!empty($mainDesc)) $descriptionParts[] = trim($mainDesc);
54
55 if (syncBasalamSettings()->getSettings(SettingsConfig::ADD_ATTR_TO_DESC_PRODUCT) === 'yes') {
56 $attributes = $this->getWooCommerceAttributes($product);
57
58 if (!empty($attributes)) {
59 $attributeTexts = [];
60
61 foreach ($attributes as $attribute) {
62 if (!empty($attribute['title']) && !empty($attribute['value'])) {
63 $attributeTexts[] = trim($attribute['title']) . ' : ' . trim($attribute['value']);
64 }
65 }
66
67 if (!empty($attributeTexts)) $descriptionParts[] = implode("\n", $attributeTexts);
68 }
69 }
70
71 $fullDescription = implode("\n\n", $descriptionParts);
72 return mb_substr($fullDescription, 0, 5000);
73 }
74
75 public function getBasalamAttributes($product): array
76 {
77 $attributes = [];
78
79 if ($this->isMobileProduct($product)) $attributes = array_merge($attributes, $this->getMobileAttributes($product));
80
81 if ($this->isGoldProduct($product)) $attributes = array_merge($attributes, $this->getGoldAttributes($product));
82
83 $wooAttributes = $this->getMappedAttributes($product);
84 if (!empty($wooAttributes)) $attributes = array_merge($attributes, $wooAttributes);
85
86 return $attributes;
87 }
88
89 private function getWooCommerceAttributes($product): array
90 {
91 $attributes = [];
92 $productAttributes = $product->get_attributes();
93
94 if (empty($productAttributes)) return [];
95
96 foreach ($productAttributes as $attribute) {
97 if ($attribute->is_taxonomy()) {
98 $taxonomy = $attribute->get_name();
99 $label = wc_attribute_label($taxonomy);
100 $terms = wc_get_product_terms($product->get_id(), $taxonomy, ['fields' => 'names']);
101 $value = implode(', ', $terms);
102 } else {
103 $label = wc_attribute_label($attribute->get_name());
104 $options = $attribute->get_options();
105 $value = implode(', ', $options);
106 }
107
108 $attributes[] = [
109 'title' => $label,
110 'value' => $value,
111 ];
112 }
113
114 return $attributes;
115 }
116
117 private function getMappedAttributes($product): array
118 {
119 $wooAttributes = $this->getWooCommerceAttributes($product);
120 $mappedOptions = $this->getMappedCategoryOptions();
121
122 if (empty($wooAttributes) || empty($mappedOptions)) return [];
123
124 foreach ($wooAttributes as &$wooAttribute) {
125 foreach ($mappedOptions as $mappedOption) {
126 if (trim($mappedOption['woo_name']) === trim($wooAttribute['title'])) {
127 $wooAttribute['title'] = $mappedOption['sync_basalam_name'];
128 break;
129 }
130 }
131 }
132
133 $basalamAttributes = $this->getBasalamAttributeDefinitions($product);
134 $matchedAttributes = [];
135
136 foreach ($wooAttributes as $wooAttribute) {
137 foreach ($basalamAttributes as $basalamAttribute) {
138 if (trim($wooAttribute['title']) === trim($basalamAttribute['title'])) {
139 $matchedAttributes[] = [
140 'attribute_id' => $basalamAttribute['id'],
141 'value' => $wooAttribute['value'],
142 ];
143 break;
144 }
145 }
146 }
147
148 return $matchedAttributes;
149 }
150
151 private function getBasalamAttributeDefinitions($product): array
152 {
153 try {
154 $categoryService = new CategoryService();
155 $categoryId = $categoryService->getPrimaryCategoryId($product);
156
157 if (!$categoryId) return [];
158
159 $response = GetCategoryAttr::getAttr($categoryId);
160 $attributes = [];
161 $responseBody = json_decode($response['body'], true);
162
163 foreach ($responseBody['data'] as $group) {
164 foreach ($group['attributes'] as $attribute) {
165 $attributes[] = [
166 'id' => $attribute['id'],
167 'title' => $attribute['title'],
168 ];
169 }
170 }
171
172 return $attributes;
173 } catch (\Exception $e) {
174 return [];
175 }
176 }
177
178 private function getMobileAttributes($product): array
179 {
180 return $this->mobileDataHandler->getMobileAttributesAsArray($product);
181 }
182
183 private function getGoldAttributes($product): array
184 {
185 return $this->goldDataHandler->getGoldAttributesAsArray($product);
186 }
187
188 private function getMappedCategoryOptions(): array
189 {
190 global $wpdb;
191 $categoryOptionsManager = new \SyncBasalam\Admin\Product\Category\CategoryOptions($wpdb);
192 return $categoryOptionsManager->getAll();
193 }
194
195 private function isMobileProduct($product): bool
196 {
197 return get_post_meta($product->get_id(), '_sync_basalam_is_mobile_product_checkbox', true) === 'yes';
198 }
199
200 private function isGoldProduct($product): bool
201 {
202 return get_post_meta($product->get_id(), '_sync_basalam_is_gold_product_checkbox', true) === 'yes';
203 }
204 }
205