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

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