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