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 / VariantService.php

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

127 lines 4.3 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
7 defined('ABSPATH') || exit;
8
9 class VariantService
10 {
11 private $priceService;
12 private array $settings;
13
14 public function __construct()
15 {
16 $this->priceService = new PriceService();
17 $this->settings = syncBasalamSettings()->getSettings();
18 }
19
20 public function getVariants($product): array
21 {
22 if (!$product instanceof \WC_Product_Variable) return [];
23
24 $variants = [];
25 $variationIds = $product->get_children();
26
27 foreach ($variationIds as $variationId) {
28 $variant = $this->createVariant($variationId, $product);
29 if ($variant) $variants[] = $variant;
30 }
31
32 return $variants;
33 }
34
35 private function createVariant(int $variationId, $parentProduct): ?array
36 {
37 $variation = wc_get_product($variationId);
38 if (!$variation) return null;
39
40 $price = $this->priceService->calculateFinalPrice($variation);
41 if (!$price) return null;
42
43 $basalamVariantId = get_post_meta($variationId, 'sync_basalam_variation_id', true);
44
45 $variantData = [
46 'primary_price' => $price,
47 'stock' => $this->getVariantStock($variation, $parentProduct),
48 'properties' => $this->getVariantProperties($variation, $parentProduct),
49 ];
50
51 $sku = trim((string) $variation->get_sku());
52 if ($sku !== '') {
53 $variantData['sku'] = $sku;
54 }
55
56 // Add Basalam variant ID if it exists
57 if (!empty($basalamVariantId)) {
58 $variantData['id'] = $basalamVariantId;
59 }
60
61 return $variantData;
62 }
63
64 private function getVariantStock($variation, $parentProduct): int
65 {
66 $defaultStock = $this->settings[SettingsConfig::DEFAULT_STOCK_QUANTITY];
67 $safeStock = $this->settings[SettingsConfig::SAFE_STOCK];
68 $stockSource = $this->settings[SettingsConfig::VARIABLE_PRODUCT_STOCK_SOURCE];
69
70 [$stock, $stockStatus] = $this->resolveStockByPriority($stockSource, $variation, $parentProduct);
71
72 $calculatedStock = $stockStatus === 'instock' ? $stock ?? $defaultStock : 0;
73
74 if ($safeStock > 0 && $calculatedStock <= $safeStock) return 0;
75
76 return $calculatedStock;
77 }
78
79 private function resolveStockByPriority(string $stockSource, $variation, $parentProduct): array
80 {
81 $preferredProduct = $stockSource === 'product' ? $parentProduct : $variation;
82 $fallbackProduct = $stockSource === 'product' ? $variation : $parentProduct;
83
84 $stock = $preferredProduct->get_stock_quantity();
85 $stockStatus = $preferredProduct->get_stock_status();
86
87 // If preferred source has no numeric stock, fallback to the other source.
88 if ($stock === null && $stockStatus === 'instock') {
89 $fallbackStock = $fallbackProduct->get_stock_quantity();
90 $fallbackStockStatus = $fallbackProduct->get_stock_status();
91
92 if ($fallbackStock !== null || $fallbackStockStatus !== 'instock') {
93 $stock = $fallbackStock;
94 $stockStatus = $fallbackStockStatus;
95 }
96 }
97
98 return [$stock, $stockStatus];
99 }
100
101 private function getVariantProperties($variation, $parentProduct): array
102 {
103 $properties = [];
104 $variationData = $variation->get_variation_attributes();
105
106 foreach ($variationData as $attributeName => $attributeValue) {
107 $taxonomyName = str_replace('attribute_', '', $attributeName);
108 $attributeLabel = str_replace(['pa_', '-'], ' ', wc_attribute_label($taxonomyName, $parentProduct));
109
110 $valueName = rawurldecode($attributeValue);
111 if (taxonomy_exists($taxonomyName)) {
112 $term = get_term_by('slug', $attributeValue, $taxonomyName);
113 if ($term && !is_wp_error($term)) {
114 $valueName = $term->name;
115 }
116 }
117
118 $properties[] = [
119 'property' => $attributeLabel,
120 'value' => str_replace('-', ' ', mb_convert_encoding($valueName, 'UTF-8', 'auto')),
121 ];
122 }
123
124 return $properties;
125 }
126 }
127