| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Strategies; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Product\Data\Handlers\ProductDataHandlerInterface; |
| 6 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 7 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 8 |
|
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
class CustomUpdateProductStrategy implements DataStrategyInterface |
| 12 |
{ |
| 13 |
public function collect($product, ProductDataHandlerInterface $handler): array |
| 14 |
{ |
| 15 |
$data = apply_filters('sync_basalam_product_payload', null, $product, $handler, 'custom_update'); |
| 16 |
if (!is_array($data)) { |
| 17 |
$data = []; |
| 18 |
} |
| 19 |
|
| 20 |
$basalamProductId = get_post_meta($product->get_id(), ProductMetaKey::basalamProductId(), true); |
| 21 |
|
| 22 |
if (!array_key_exists('id', $data)) { |
| 23 |
$data['id'] = $basalamProductId; |
| 24 |
} |
| 25 |
|
| 26 |
if (!array_key_exists('name', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_NAME)) { |
| 27 |
$data['name'] = $handler->getName($product); |
| 28 |
} |
| 29 |
|
| 30 |
if (!array_key_exists('photo', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) { |
| 31 |
$data['photo'] = $handler->getMainPhoto($product); |
| 32 |
} |
| 33 |
if (!array_key_exists('photos', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PHOTOS)) { |
| 34 |
$data['photos'] = $handler->getGalleryPhotos($product); |
| 35 |
} |
| 36 |
|
| 37 |
if (!array_key_exists('video', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_VIDEO)) { |
| 38 |
$video = $handler->getVideo($product); |
| 39 |
if ($video !== null) { |
| 40 |
$data['video'] = $video; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (!array_key_exists('primary_price', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) { |
| 45 |
if (!$product->is_type('variable')) { |
| 46 |
$data['primary_price'] = $handler->getPrice($product); |
| 47 |
} |
| 48 |
} |
| 49 |
if (!array_key_exists('variants', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_PRICE)) { |
| 50 |
$data['variants'] = $handler->getVariants($product); |
| 51 |
} |
| 52 |
|
| 53 |
if (!array_key_exists('stock', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) { |
| 54 |
if (!$product->is_type('variable')) { |
| 55 |
$data['stock'] = $handler->getStock($product); |
| 56 |
} |
| 57 |
} |
| 58 |
if (!array_key_exists('variants', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_STOCK)) { |
| 59 |
$data['variants'] = $handler->getVariants($product); |
| 60 |
} |
| 61 |
|
| 62 |
if (!array_key_exists('weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) { |
| 63 |
$data['weight'] = $handler->getWeight($product); |
| 64 |
} |
| 65 |
if (!array_key_exists('package_weight', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_WEIGHT)) { |
| 66 |
$data['package_weight'] = $handler->getPackageWeight($product); |
| 67 |
} |
| 68 |
|
| 69 |
if (!array_key_exists('description', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_DESCRIPTION)) { |
| 70 |
$data['description'] = $handler->getDescription($product); |
| 71 |
} |
| 72 |
|
| 73 |
if (!array_key_exists('product_attribute', $data) && $this->shouldSyncField(SettingsConfig::SYNC_PRODUCT_FIELD_ATTR)) { |
| 74 |
$data['product_attribute'] = $handler->getAttributes($product); |
| 75 |
} |
| 76 |
|
| 77 |
if (!array_key_exists('variants', $data)) { |
| 78 |
$data['variants'] = []; |
| 79 |
} |
| 80 |
|
| 81 |
return array_filter($data, fn($value) => $value !== null); |
| 82 |
} |
| 83 |
|
| 84 |
private function shouldSyncField(string $fieldKey): bool |
| 85 |
{ |
| 86 |
$setting = syncBasalamSettings()->getSettings($fieldKey); |
| 87 |
return $setting == true || $setting === '1' || $setting === 1; |
| 88 |
} |
| 89 |
} |
| 90 |
|