<?php
// Skip time limit for big updates
set_time_limit(0);
// Startup
require_once('./startup.php');
// Init response array
$response = array();
// Get global settings
$configdb = (array)$settings->getSetting('config');
// Actions
if ( !empty( $_POST ) )
{
if ( $_POST['action'] === 'update' ) { //UPDATE PRODUCTS
// Get import settings
$xml = simplexml_load_string( file_get_contents('./settings.xml'), 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS );
if ( !$xml ) {
$response = array(
'status' => 0,
'detail' => 'НЕ УДАЛОСЬ ПОЛУЧИТЬ ФАЙЛ НАСТРОЕК'
);
} else {
$settings = json_decode( json_encode( $xml ), true );
array_walk( $settings, 'trim' );
$data = @json_decode( $_POST['data'], true );
// init groups sync
if ( !empty( $data ) ) {
foreach ( $data as $product ) {
//trim data
$product['id'] = trim( $product['id'] );
$product['name'] = array_map('trim', $product['name'] );
$product['code'] = trim( $product['code'] );
// step 0 - check consistent upload
if ( empty( $product['id'] ) || empty( $product['name'] ) ) {
continue;
}
//step 1 — check existing sync code
$existing_id = $productex->getSyncedProduct( $product['id'] );
//check parent
$categories = array();
$categories[] = $categoryex->getSyncedCategory( $product['category'] );
/*if ( !empty( $product['category'] ) ) {
foreach ( $product['category'] as $category_id ) {
}
}*/
//compose data array
$data = array (
'sync_id' => $product['id'],
'product_description' => array (
8 => array (
'name' => $product['name']['ru'],
'seo_h1' => '',
'seo_title' => '',
'meta_keyword' => '',
'meta_description' => '',
'description' => $product['description']['ru'],
'tag' => '',
),
9 => array (
'name' => $product['name']['uk'],
'seo_h1' => '',
'seo_title' => '',
'meta_keyword' => '',
'meta_description' => '',
'description' => $product['description']['uk'],
'tag' => '',
),
),
'model' => !empty($product['code']) ? $product['code'] : $product['id'],
'sku' => $product['id'],
'upc' => '',
'ean' => '',
'jan' => '',
'isbn' => '',
'mpn' => '',
'location' => '',
//'price' => $product['price'],
//'quantity' => $product['stock'],
'tax_class_id' => '0',
'minimum' => '1',
'subtract' => '1',
'stock_status_id' => isset($configdb['config_stock_status_id']) ? $configdb['config_stock_status_id'] : '5',
'shipping' => '1',
'keyword' => $translit->toTranslit( $product['name']['uk'] . '-' . $product['id'] ),
//'image' => 'no_image.jpg',
'date_available' => date('Y-m-d'),
'length' => '',
'width' => '',
'height' => '',
'length_class_id' => '1',
'weight' => isset($product['weight']) ? $product['weight'] : 0,
'weight_class_id' => '1',
// 'status' => ($product['stock'] > 0) ? 1 : 0,
'sort_order' => '1',
'manufacturer_id' => '0',
'filter' => '',
'product_store' => array (
0 => '0',
),
'download' => '',
'related' => '',
'option' => '',
'points' => '',
'product_reward' => array (
1 => array (
'points' => '',
),
),
'product_layout' => array (
0 => array (
'layout_id' => '',
),
),
);
if ( !empty( $categories ) ) {
$data['product_category'] = $categories;
} else {
$data['product_category'] = array();
}
if ( !empty( $product['manufacturer'] ) ) {
$manufacturer_id = $manufacturerex->getSyncedManufacturer( trim( $product['manufacturer'] ) );
if ( $manufacturer_id )
$data['manufacturer_id'] = $manufacturer_id;
}
if ( $existing_id ) {
$productex->editProduct( $existing_id, $data );
} else {
$data['image'] = 'no_image.jpg';
$productex->addProduct( $data );
}
// check alias
if ( !empty( $product['alias'] ) ) {
foreach ( $product['alias'] as $alias => $show ) {
$productex->saveAlias( $product['id'], $alias, $show );
}
}
}
$response = array(
'status' => 1,
'detail' => 'ОБНОВЛЕНИЕ ТОВАРА ЗАВЕРШЕНО'
);
} else {
$response = array(
'status' => 0,
'detail' => 'СПИСОК ТОВАРОВ ПУСТ'
);
}
}
} else if ( $_POST['action'] === 'price' ) { //UPDATE PRICE
$data = @json_decode( $_POST['data'], true );
// init groups sync
if ( !empty( $data ) ) {
$skipped = 0;
foreach ( $data as $product ) {
$product['id'] = trim( $product['id'] );
// step 0 - check consistent upload
if ( empty( $product['id'] ) ) {
$skipped++; continue;
}
//step 1 — check existing sync code
$existing_id = $productex->getSyncedProduct( $product['id'] );
if ( $existing_id ) {
$productex->clearProductPriceSpecial( $existing_id );
if (isset($product['special']) && $product['special'] == true) {
if (isset($product['roz'])) {
$p = $productex->getProduct($existing_id);
if (empty($p['price'])) {
$price = $product['roz'] * 1.1;
$productex->editProductPrice( $existing_id, $price );
}
$productex->editProductPriceSpecial( $existing_id, 1, $product['roz'] );
}
if (isset($product['opt']))
$productex->editProductPriceSpecial( $existing_id, 2, $product['opt'] );
} else {
$productex->editProductPrice( $existing_id, $product['roz'] );
$productex->editProductPriceDiscount( $existing_id, 2, $product['opt'] );
}
} else {
$skipped++;
}
}
$response = array(
'status' => 1,
'detail' => 'ЦЕНЫ ТОВАРОВ ОБНОВЛЕНЫ (ПРОПУЩЕНО ТОВАРОВ: ' . $skipped . ')'
);
} else {
$response = array(
'status' => 0,
'detail' => 'СПИСОК ТОВАРОВ ПУСТ'
);
}
} else if ( $_POST['action'] === 'stock' ) { //UPDATE STOCK
$data = @json_decode( $_POST['data'], true );
// init groups sync
if ( !empty( $data ) ) {
$skipped = 0;
foreach ( $data as $product ) {
$product['id'] = trim( $product['id'] );
// step 0 - check consistent upload
if ( empty( $product['id'] ) || $product['qty'] <= 0) {
$skipped++; continue;
}
//step 1 — check existing sync code
$existing_id = $productex->getSyncedProduct( $product['id'] );
if ( $existing_id ) {
$productex->editProductQty( $existing_id, $product['qty'] );
} else {
$skipped++;
}
}
$response = array(
'status' => 1,
'detail' => 'ОСТАТКИ ТОВАРОВ ОБНОВЛЕНЫ (ПРОПУЩЕНО ТОВАРОВ: ' . $skipped . ')'
);
} else {
$response = array(
'status' => 0,
'detail' => 'СПИСОК ТОВАРОВ ПУСТ'
);
}
} else if ( $_POST['action'] === 'synonym' ) { //UPDATE SYNONYMS
$data = @json_decode( $_POST['data'], true );
// init groups sync
if ( !empty( $data ) ) {
$productex->clearSynonyms();
foreach ( $data as $item ) {
$original = key( $item );
$synonym = current( $item );
$productex->saveSynonym( $original, $synonym );
}
$response = array(
'status' => 1,
'detail' => 'СИНОНИМЫ ОБНОВЛЕНЫ'
);
} else {
$response = array(
'status' => 0,
'detail' => 'СПИСОК СИНОНИМОВ ПУСТ'
);
}
} else if ( $_POST['action'] === 'details' ) { //UPDATE DETAILS
// Get import settings
$xml = simplexml_load_string( file_get_contents('./settings.xml'), 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS );
if ( !$xml ) {
$response = array(
'status' => 0,
'detail' => 'НЕ УДАЛОСЬ ПОЛУЧИТЬ ФАЙЛ НАСТРОЕК'
);
} else {
$settings = json_decode( json_encode( $xml ), true );
array_walk( $settings, 'trim' );
$data = @json_decode( $_POST['data'], true );
// init groups sync
if ( !empty( $data ) ) {
if ( !empty( $data['description'] ) ) {
foreach ( $data['description'] as $sync_id => $description ) {
$sync_id = trim( $sync_id );
$existing_id = $productex->getSyncedProduct( $sync_id );
if ( !empty( $existing_id ) ) {
$productex->editProductDescription( $existing_id, trim( $description ) );
}
}
}
if ( !empty( $data['meta_keywords'] ) ) {
foreach ( $data['meta_keywords'] as $sync_id => $meta_keywords ) {
$existing_id = $productex->getSyncedProduct( $sync_id );
if ( !empty( $existing_id ) ) {
$productex->editProductMetaKeywords( $existing_id, trim( $meta_keywords ) );
}
}
}
$response = array(
'status' => 1,
'detail' => 'ДЕТАЛИ ТОВАРА ОБНОВЛЕНЫ'
);
} else {
$response = array(
'status' => 0,
'detail' => 'СПИСОК ДЕТАЛЕЙ ТОВАРА ПУСТ'
);
}
}
} else if ( $_POST['action'] === 'clear' ) { //DISABLE PRODUCTS
$productex->disableProducts();
$productex->clearAliases();
$response = array(
'status' => 1,
'detail' => 'ВСЕ ТОВАРЫ ОТКЛЮЧЕНЫ'
);
} else if ( $_POST['action'] === 'clean' ) { //CLEAN MODEL
$r = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
mysql_select_db( DB_DATABASE, $r );
$result = mysql_query( 'SELECT product_id, model FROM oc_product WHERE `sku` = "" ORDER BY product_id LIMIT 10000', $r );
while ( $row = mysql_fetch_assoc($result) ) {
mysql_query('UPDATE oc_product SET `sku` = "' . $productex->cleanModel( $row['model'] ) . '" WHERE product_id = "' . $row['product_id'] . '"');
}
die('finish');
} else {
$response = array(
'status' => 0,
'detail' => 'НЕВЕРНЫЙ ПАРАМЕТР ACTION'
);
}
} else {
$response = array(
'status' => 0,
'detail' => 'НЕВЕРНЫЙ ЗАПРОС'
);
}
sendResponse( $response );
мне кстати сюда еще допилить выгрузку атрибутов нужно, не могу разобраться