| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class FetchUnsyncProducts |
| 10 |
{ |
| 11 |
private $getProductsService; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->getProductsService = new FetchProductsData(); |
| 16 |
} |
| 17 |
|
| 18 |
public function getUnsyncBasalamProducts($cursor = null, &$nextCursor = null) |
| 19 |
{ |
| 20 |
$currentCursor = $cursor; |
| 21 |
|
| 22 |
while (true) { |
| 23 |
$productData = $this->getProductsService->getProductData(null, $currentCursor); |
| 24 |
$nextCursor = $productData['next_cursor'] ?? null; |
| 25 |
|
| 26 |
$basalamProducts = isset($productData['data']) && is_array($productData['data']) ? $productData['data'] : []; |
| 27 |
if (empty($basalamProducts)) return []; |
| 28 |
|
| 29 |
$products = []; |
| 30 |
|
| 31 |
foreach ($basalamProducts as $product) { |
| 32 |
if (!is_array($product) || empty($product['id'])) continue; |
| 33 |
|
| 34 |
if (!get_posts([ |
| 35 |
'post_type' => 'product', |
| 36 |
'meta_key' => ProductMetaKey::basalamProductId(), |
| 37 |
'meta_value' => $product['id'], |
| 38 |
'fields' => 'ids', |
| 39 |
])) { |
| 40 |
$products[] = $product; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (!empty($products)) return $products; |
| 45 |
|
| 46 |
if (empty($productData['has_more']) || $nextCursor === null) return []; |
| 47 |
|
| 48 |
$currentCursor = $nextCursor; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|