# sync-basalam/1.9.1/includes/Services/Orders/SyncOrderService.php

ووسلام – همگام سازی ووکامرس و باسلام, version 1.9.1. 78 lines.

- Page: https://pluginprobe.com/plugins/sync-basalam/1.9.1/code/includes/Services/Orders/SyncOrderService.php
- Raw: https://pluginprobe.com/plugins/sync-basalam/1.9.1/raw/includes/Services/Orders/SyncOrderService.php
- Modified: 2026-06-30T10:43:26+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/sync-basalam/1.9.1/code/includes/Services/Orders/SyncOrderService.php#L10-L20`.

```php
<?php

namespace SyncBasalam\Services\Orders;

use SyncBasalam\Logger\Logger;

defined('ABSPATH') || exit;

class SyncOrderService
{
    public function syncOrders(array $orders): array
    {
        $synced = 0;
        $skipped = 0;
        $errors = [];

        if (empty($orders)) {
            return [
                'synced' => 0,
                'skipped' => 0,
                'errors' => []
            ];
        }

        global $wpdb;
        $tableName = $wpdb->prefix . 'sync_basalam_payments';

        foreach ($orders as $order) {
            $invoiceId = $order['order']['id'] ?? null;

            if (!$invoiceId) {
                $message = "سفارش بدون شناسه فاکتور پیدا شد: " . wp_json_encode($order, JSON_UNESCAPED_UNICODE);
                Logger::warning($message);
                continue;
            }

            $exists = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT invoice_id FROM {$tableName} WHERE invoice_id = %d",
                    $invoiceId
                )
            );

            if ($exists) {
                $skipped++;
                continue;
            }

            try {
                $result = OrderManager::createOrderWoo([
                    'invoice_id'  => $order['order']['id'],
                    'user_id'     => $order['order']['customer']['user']['id'] ?? null,
                    'city_id'     => $order['order']['customer']['city']['id'] ?? null,
                    'province_id' => $order['order']['customer']['city']['parent']['id'] ?? null,
                ]);

                if (empty($result['success'])) {
                    $errorMsg = $result['error'] ?? 'خطای نامشخص';
                    Logger::error("خطا در ایجاد سفارش {$invoiceId}: " . $errorMsg);
                    $errors[] = "سفارش {$invoiceId}: " . $errorMsg;
                } else {
                    $synced++;
                    Logger::info("سفارش {$invoiceId} با موفقیت ایجاد شد.");
                }
            } catch (\Exception $e) {
                Logger::error("خطا در ایجاد سفارش {$invoiceId}: " . $e->getMessage());
                $errors[] = "سفارش {$invoiceId}: " . $e->getMessage();
            }
        }

        return [
            'synced' => $synced,
            'skipped' => $skipped,
            'errors' => $errors
        ];
    }
}

```
