Created
August 6, 2014 17:26
-
-
Save vgrish/3316f0d2135e505bcf7f to your computer and use it in GitHub Desktop.
обновление цен и остатка по крону
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MODX_API_MODE', true); | |
require_once '../index.php'; // подключаем index.php | |
$modx->getService('error','error.modError'); | |
$modx->getRequest(); | |
$modx->setLogLevel(modX::LOG_LEVEL_ERROR); | |
$modx->setLogTarget('FILE'); | |
$modx->error->message = null; | |
$fields = 'article,price,amount'; // импортируемые поля | |
$file = 'ttt.csv'; // твой файл импорта | |
$delimeter = ';'; // разделитель | |
$key = 'article'; // уникальное поле, у нас article | |
$keys = array_map('trim', explode(',', strtolower($fields))); | |
$tv_enabled = false; | |
foreach ($keys as $v) { | |
if (preg_match('/^tv(\d)$/', $v)) { | |
$tv_enabled = true; | |
break; | |
} | |
} | |
// Import! | |
$handle = fopen($file, "r"); | |
$rows = $created = $updated = 0; | |
while (($csv = fgetcsv($handle, 0, $delimeter)) !== false) { | |
$rows ++; | |
$modx->error->reset(); | |
$modx->log(modX::LOG_LEVEL_INFO, "Raw data for import: \n".print_r($csv,1)); | |
foreach ($keys as $k => $v) { | |
if (!isset($csv[$k])) { | |
exit('Field "' . $v . '" not exists in file. Please fix import file or parameter "fields".'); | |
} | |
elseif (isset($data[$v]) && !is_array($data[$v])) { | |
$data[$v] = array($data[$v], $csv[$k]); | |
} | |
elseif (isset($data[$v]) && is_array($data[$v])) { | |
$data[$v][] = $csv[$k]; | |
} | |
else { | |
$data[$v] = $csv[$k]; | |
} | |
} | |
} | |
fclose($handle); | |
$i = 0; | |
$array1 = array(','); | |
$array2 = array('.'); | |
for ($i=0; $i < count($data[$key]); $i++) { | |
$price = str_replace($array1,$array2,$data[$keys[1]][$i]); // убираем запятую | |
$amount = $data[$keys[2]][$i]; | |
$sql = "UPDATE ".$modx->getTableName('msProductData')." SET `".$keys[1]."` = ".$price." WHERE ".$key." LIKE '".$data[$keys[0]][$i]."' "; | |
$q = $modx->prepare($sql); | |
$q->execute(); | |
$sql = "UPDATE ".$modx->getTableName('msProductData')." SET `".$keys[2]."` = ".$amount." WHERE ".$key." LIKE '".$data[$keys[0]][$i]."' "; | |
$q = $modx->prepare($sql); | |
$q->execute(); | |
} | |
$modx->cacheManager->refresh(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment