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

122 lines 4.1 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 // Add Basalam variant ID if it exists
52 if (!empty($basalamVariantId)) {
53 $variantData['id'] = $basalamVariantId;
54 }
55
56 return $variantData;
57 }
58
59 private function getVariantStock($variation, $parentProduct): int
60 {
61 $defaultStock = $this->settings[SettingsConfig::DEFAULT_STOCK_QUANTITY];
62 $safeStock = $this->settings[SettingsConfig::SAFE_STOCK];
63 $stockSource = $this->settings[SettingsConfig::VARIABLE_PRODUCT_STOCK_SOURCE];
64
65 [$stock, $stockStatus] = $this->resolveStockByPriority($stockSource, $variation, $parentProduct);
66
67 $calculatedStock = $stockStatus === 'instock' ? $stock ?? $defaultStock : 0;
68
69 if ($safeStock > 0 && $calculatedStock <= $safeStock) return 0;
70
71 return $calculatedStock;
72 }
73
74 private function resolveStockByPriority(string $stockSource, $variation, $parentProduct): array
75 {
76 $preferredProduct = $stockSource === 'product' ? $parentProduct : $variation;
77 $fallbackProduct = $stockSource === 'product' ? $variation : $parentProduct;
78
79 $stock = $preferredProduct->get_stock_quantity();
80 $stockStatus = $preferredProduct->get_stock_status();
81
82 // If preferred source has no numeric stock, fallback to the other source.
83 if ($stock === null && $stockStatus === 'instock') {
84 $fallbackStock = $fallbackProduct->get_stock_quantity();
85 $fallbackStockStatus = $fallbackProduct->get_stock_status();
86
87 if ($fallbackStock !== null || $fallbackStockStatus !== 'instock') {
88 $stock = $fallbackStock;
89 $stockStatus = $fallbackStockStatus;
90 }
91 }
92
93 return [$stock, $stockStatus];
94 }
95
96 private function getVariantProperties($variation, $parentProduct): array
97 {
98 $properties = [];
99 $variationData = $variation->get_variation_attributes();
100
101 foreach ($variationData as $attributeName => $attributeValue) {
102 $taxonomyName = str_replace('attribute_', '', $attributeName);
103 $attributeLabel = str_replace(['pa_', '-'], ' ', wc_attribute_label($taxonomyName, $parentProduct));
104
105 $valueName = rawurldecode($attributeValue);
106 if (taxonomy_exists($taxonomyName)) {
107 $term = get_term_by('slug', $attributeValue, $taxonomyName);
108 if ($term && !is_wp_error($term)) {
109 $valueName = $term->name;
110 }
111 }
112
113 $properties[] = [
114 'property' => $attributeLabel,
115 'value' => str_replace('-', ' ', mb_convert_encoding($valueName, 'UTF-8', 'auto')),
116 ];
117 }
118
119 return $properties;
120 }
121 }
122