| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Handlers; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Data\Services\VariantService; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class VariableProductHandler extends SimpleProductHandler |
| 10 |
{ |
| 11 |
private $variantService; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
parent::__construct(); |
| 16 |
$this->variantService = new VariantService(); |
| 17 |
} |
| 18 |
|
| 19 |
public function getVariants($product): array |
| 20 |
{ |
| 21 |
return $this->variantService->getVariants($product); |
| 22 |
} |
| 23 |
|
| 24 |
public function getSku($product): ?string |
| 25 |
{ |
| 26 |
$sku = trim((string) $product->get_sku()); |
| 27 |
if ($sku !== '') return $sku; |
| 28 |
|
| 29 |
// Variable products keep their SKUs at variation level (the parent SKU field |
| 30 |
// is usually empty in WooCommerce), so resolve the product-level SKU from |
| 31 |
// the first variation that has one. |
| 32 |
foreach ($product->get_children() as $variationId) { |
| 33 |
$variation = \wc_get_product($variationId); |
| 34 |
if (!$variation) continue; |
| 35 |
|
| 36 |
$variationSku = trim((string) $variation->get_sku()); |
| 37 |
if ($variationSku !== '') return $variationSku; |
| 38 |
} |
| 39 |
|
| 40 |
return null; |
| 41 |
} |
| 42 |
} |