Deploy Ladill Woo Manager / deploy (push) Successful in 35s
Sync currency from the plugin, format product prices and totals with it, and show the store currency on product price fields instead of hard-coding GHS.
52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Woo;
|
|
|
|
use App\Models\WooStore;
|
|
|
|
class OrderSyncService
|
|
{
|
|
public function __construct(
|
|
private PluginClient $client,
|
|
private OrderIngestService $orders,
|
|
private CatalogSyncService $catalog,
|
|
) {}
|
|
|
|
public function syncOrders(WooStore $store): int
|
|
{
|
|
$response = $this->client->get($store, 'orders', ['per_page' => 100]);
|
|
if (! is_array($response)) {
|
|
return 0;
|
|
}
|
|
|
|
if (isset($response['currency']) || isset($response['currency_symbol'])) {
|
|
$this->catalog->applyStoreMeta($store, $response);
|
|
}
|
|
|
|
$items = $response['data'] ?? $response;
|
|
if (! is_array($items)) {
|
|
return 0;
|
|
}
|
|
|
|
if (array_is_list($items) === false && isset($response['data']) === false) {
|
|
$items = array_values(array_filter($items, fn ($item) => is_array($item) && (isset($item['id']) || isset($item['number']))));
|
|
}
|
|
|
|
$count = 0;
|
|
foreach ($items as $item) {
|
|
if (! is_array($item)) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$this->orders->ingest($store, $item);
|
|
$count++;
|
|
} catch (\Throwable) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
}
|