| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Handlers; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Data\Services\CategoryService; |
| 6 |
use SyncBasalam\Admin\Product\Data\Services\PriceService; |
| 7 |
use SyncBasalam\Admin\Product\Data\Services\PhotoService; |
| 8 |
use SyncBasalam\Admin\Product\Data\Services\VideoService; |
| 9 |
use SyncBasalam\Admin\Product\Data\Services\AttributeService; |
| 10 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 11 |
use SyncBasalam\Services\Products\PreparationDaysGuard; |
| 12 |
|
| 13 |
defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
class SimpleProductHandler implements ProductDataHandlerInterface |
| 16 |
{ |
| 17 |
private $categoryService; |
| 18 |
private $priceService; |
| 19 |
private $photoService; |
| 20 |
private $videoService; |
| 21 |
private $attributeService; |
| 22 |
private $preparationGuard; |
| 23 |
private array $settings; |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
$this->categoryService = new CategoryService(); |
| 28 |
$this->priceService = new PriceService(); |
| 29 |
$this->photoService = new PhotoService(); |
| 30 |
$this->videoService = new VideoService(); |
| 31 |
$this->attributeService = new AttributeService(); |
| 32 |
$this->preparationGuard = new PreparationDaysGuard(); |
| 33 |
$this->settings = syncBasalamSettings()->getSettings(); |
| 34 |
} |
| 35 |
|
| 36 |
public function getName($product): string |
| 37 |
{ |
| 38 |
$baseName = $product->get_name(); |
| 39 |
|
| 40 |
$prefix = $this->settings[SettingsConfig::PRODUCT_PREFIX_TITLE]; |
| 41 |
$suffix = $this->settings[SettingsConfig::PRODUCT_SUFFIX_TITLE]; |
| 42 |
|
| 43 |
$name = $prefix ? "{$prefix} {$baseName}" : $baseName; |
| 44 |
$name = $suffix ? "{$name} {$suffix}" : $name; |
| 45 |
|
| 46 |
$attributeSuffix = $this->attributeService->getAttributeSuffix($product); |
| 47 |
if ($attributeSuffix) $name .= " ({$attributeSuffix})"; |
| 48 |
|
| 49 |
return mb_substr($name, 0, 120); |
| 50 |
} |
| 51 |
|
| 52 |
public function getDescription($product): string |
| 53 |
{ |
| 54 |
return $this->attributeService->generateDescription($product); |
| 55 |
} |
| 56 |
|
| 57 |
public function getCategoryId($product): ?int |
| 58 |
{ |
| 59 |
return $this->categoryService->getPrimaryCategoryId($product); |
| 60 |
} |
| 61 |
|
| 62 |
public function getCategoryIds($product): array |
| 63 |
{ |
| 64 |
return $this->categoryService->getCategoryIds($product); |
| 65 |
} |
| 66 |
|
| 67 |
public function getPrice($product): ?int |
| 68 |
{ |
| 69 |
return $this->priceService->calculateFinalPrice($product); |
| 70 |
} |
| 71 |
|
| 72 |
public function getStock($product): int |
| 73 |
{ |
| 74 |
$defaultStock = $this->settings[SettingsConfig::DEFAULT_STOCK_QUANTITY]; |
| 75 |
$safeStock = $this->settings[SettingsConfig::SAFE_STOCK]; |
| 76 |
$stock = $product->get_stock_quantity(); |
| 77 |
$stockStatus = $product->get_stock_status(); |
| 78 |
|
| 79 |
$calculatedStock = $stockStatus === 'instock' ? $stock ?? $defaultStock : 0; |
| 80 |
|
| 81 |
if ($safeStock > 0 && $calculatedStock <= $safeStock) return 0; |
| 82 |
|
| 83 |
return $calculatedStock; |
| 84 |
} |
| 85 |
|
| 86 |
public function getWeight($product): ?int |
| 87 |
{ |
| 88 |
if (empty($product->get_weight())) return $this->settings[SettingsConfig::DEFAULT_WEIGHT]; |
| 89 |
|
| 90 |
$weight = str_replace(',', '.', $product->get_weight()); |
| 91 |
$weightUnit = get_option('woocommerce_weight_unit'); |
| 92 |
|
| 93 |
return ($weightUnit === 'kg') ? floatval($weight) * 1000 : floatval($weight); |
| 94 |
} |
| 95 |
|
| 96 |
public function getPackageWeight($product): int |
| 97 |
{ |
| 98 |
$weight = $this->getWeight($product) ?? 0; |
| 99 |
$packageWeight = $this->settings[SettingsConfig::DEFAULT_PACKAGE_WEIGHT]; |
| 100 |
|
| 101 |
return intval($weight + $packageWeight); |
| 102 |
} |
| 103 |
|
| 104 |
public function getMainPhoto($product): ?int |
| 105 |
{ |
| 106 |
return $this->photoService->getMainPhotoId($product); |
| 107 |
} |
| 108 |
|
| 109 |
public function getGalleryPhotos($product): array |
| 110 |
{ |
| 111 |
return $this->photoService->getGalleryPhotoIds($product); |
| 112 |
} |
| 113 |
|
| 114 |
public function getVideo($product): ?int |
| 115 |
{ |
| 116 |
return $this->videoService->getVideoFileId($product); |
| 117 |
} |
| 118 |
|
| 119 |
public function getPreparationDays($product): int |
| 120 |
{ |
| 121 |
$preparationDays = (int) $this->settings[SettingsConfig::DEFAULT_PREPARATION]; |
| 122 |
|
| 123 |
return $this->preparationGuard->enforce($preparationDays, $product); |
| 124 |
} |
| 125 |
|
| 126 |
public function getUnitType($product): int |
| 127 |
{ |
| 128 |
$unitType = get_post_meta($product->get_id(), '_sync_basalam_product_unit', true); |
| 129 |
return ($unitType && is_numeric($unitType)) ? intval($unitType) : 6304; |
| 130 |
} |
| 131 |
|
| 132 |
public function getUnitQuantity($product): int |
| 133 |
{ |
| 134 |
$quantity = get_post_meta($product->get_id(), '_sync_basalam_product_value', true); |
| 135 |
return is_numeric($quantity) ? intval($quantity) : 1; |
| 136 |
} |
| 137 |
|
| 138 |
public function isWholesale($product): bool |
| 139 |
{ |
| 140 |
$globalSetting = $this->settings[SettingsConfig::ALL_PRODUCTS_WHOLESALE]; |
| 141 |
|
| 142 |
if ($globalSetting === 'all') return true; |
| 143 |
|
| 144 |
return get_post_meta($product->get_id(), '_sync_basalam_is_wholesale', true) === 'yes'; |
| 145 |
} |
| 146 |
|
| 147 |
public function getVariants($product): array |
| 148 |
{ |
| 149 |
return []; |
| 150 |
} |
| 151 |
|
| 152 |
public function getAttributes($product): array |
| 153 |
{ |
| 154 |
return $this->attributeService->getBasalamAttributes($product); |
| 155 |
} |
| 156 |
} |
| 157 |
|