Інтеграція 1С з OpenCart
Синхронізація каталогу, залишків і замовлень між 1С і OpenCart — стандартне завдання для інтернет-магазинів з обліковою системою. Дані повинні текти в обидві сторони: товари з 1С у OpenCart, замовлення з OpenCart у 1С.
Підходи до інтеграції
CommerceML (стандарт 1С) — 1С вміє генерувати XML-файли у форматі CommerceML 2 й обмінюватися ними з веб-сайтом по HTTP. Найсумісніший метод.
REST API — 1С Підприємство 8.3 підтримує HTTP-сервіси. OpenCart надає REST API. Більш гнучко, але вимагає програмування на обох сторонах.
Пряме підключення до БД — не рекомендується в production, але використовується для одноразових міграцій.
Інтеграція CommerceML
Протокол: 1С надсилає POST-запити до спеціального скрипту на веб-сайті. OpenCart повинен мати обробник /index.php?route=api/1c/....
Встановити модуль: 1C-Bitrix Exchange (open source) або ocStore Exchange 1C.
Конфігурація у 1С (Обмін даними з веб-сайтом):
URL веб-сайту: https://shop.ru/index.php?route=api/1c
Користувач: API-користувач OpenCart
Пароль: ****
Періодичність: кожні 15 хвилин (залишки), щогодини (повний каталог)
Кастомний обробник CommerceML
// catalog/controller/api/exchange1c.php
class ControllerApi1cExchange extends Controller {
private function authenticate(): bool {
$token = $this->request->get['token'] ?? $this->request->server['HTTP_X_API_TOKEN'] ?? '';
return hash_equals($this->config->get('api_1c_token'), $token);
}
public function catalog(): void {
if (!$this->authenticate()) {
$this->response->setOutput('failure=Unauthorized');
return;
}
$mode = $this->request->get['mode'] ?? '';
match ($mode) {
'checkauth' => $this->checkAuth(),
'init' => $this->init(),
'file' => $this->receiveFile(),
'import' => $this->import(),
default => $this->response->setOutput('failure=Unknown mode'),
};
}
private function import(): void {
$filename = $this->request->get['filename'] ?? '';
$filePath = DIR_UPLOAD . 'exchange1c/' . basename($filename);
if (!file_exists($filePath)) {
$this->response->setOutput('failure=File not found');
return;
}
$xml = simplexml_load_file($filePath);
$this->processProducts($xml);
$this->response->setOutput('success=Import completed');
}
private function processProducts(\SimpleXMLElement $xml): void {
foreach ($xml->Каталог->Товари->Товар as $product) {
$sku = (string)$product->Артикул;
$name = (string)$product->Найменування;
$price = (float)$product->ЦінаЗаЕдиницю;
$existingId = $this->getProductIdBySku($sku);
if ($existingId) {
$this->model_catalog_product->editProduct($existingId, [
'price' => $price,
'quantity' => (int)$product->Залишок,
]);
} else {
$this->model_catalog_product->addProduct([
'sku' => $sku,
'model' => $sku,
'name' => ['ru' => $name],
'price' => $price,
'quantity' => (int)$product->Залишок,
'status' => 1,
]);
}
}
}
}
Синхронізація залишків (швидкий режим)
Для частого оновлення залишків (кожні 5–15 хвилин) — окремий легкий endpoint:
// POST /api/1c/stock
// Body: JSON [{sku: "ART-001", qty: 15}, ...]
public function updateStock(): void {
$items = json_decode($this->request->post['data'], true);
$updated = 0;
foreach ($items as $item) {
$productId = $this->getProductIdBySku($item['sku']);
if ($productId) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET quantity = '" . (int)$item['qty'] . "'
WHERE product_id = '" . (int)$productId . "'");
$updated++;
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode(['updated' => $updated]));
}
Експорт замовлень у 1С
// GET /api/1c/orders?from=2024-01-01&status=2
public function getOrders(): void {
$dateFrom = $this->request->get['from'] ?? date('Y-m-d', strtotime('-1 day'));
$statusId = (int)($this->request->get['status'] ?? 2); // 2 = Processing
$orders = $this->model_sale_order->getOrders([
'filter_date_added' => $dateFrom,
'filter_order_status_id' => $statusId,
]);
$result = [];
foreach ($orders as $order) {
$products = $this->model_sale_order->getOrderProducts($order['order_id']);
$result[] = [
'id' => $order['order_id'],
'date' => $order['date_added'],
'total' => $order['total'],
'customer' => $order['firstname'] . ' ' . $order['lastname'],
'phone' => $order['telephone'],
'address' => $order['shipping_address_1'],
'products' => array_map(fn($p) => [
'sku' => $p['model'],
'name' => $p['name'],
'qty' => $p['quantity'],
'price' => $p['price'],
], $products),
];
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode(['orders' => $result]));
}
Обробка помилок і черга
Для надійності — асинхронна черга. Якщо 1С недоступна, зміни ставляться в чергу:
// Таблиця черги
CREATE TABLE oc_1c_queue (
id INT AUTO_INCREMENT PRIMARY KEY,
type ENUM('product', 'stock', 'order') NOT NULL,
payload JSON NOT NULL,
status ENUM('pending', 'processing', 'done', 'failed') DEFAULT 'pending',
attempts INT DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
processed_at DATETIME NULL
);
Терміни
Базова інтеграція (каталог + залишки через CommerceML) — 5–7 днів. Двостороння інтеграція з замовленнями й чергою — 10–14 днів.







