Last active
May 31, 2016 11:19
-
-
Save Burick/d808b64f0f4fcf51ab819b3b313e9d81 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 | |
/* | |
1605/01;иван;носки 1шт 100р,трусы 1шт 200р;Москва, ул Альбина д5; хочу побыстрее | |
*/ | |
$index = $_SERVER[DOCUMENT_ROOT].'/index.php'; | |
// Подключаем | |
define('MODX_API_MODE', true); | |
//require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/index.php'; | |
require_once $index; | |
// Включаем обработку ошибок | |
$modx->getService('error','error.modError'); | |
$modx->setLogLevel(modX::LOG_LEVEL_INFO); | |
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML'); | |
//$ms2ModelPath = MODX_CORE_PATH . 'components/minishop2/model/'; | |
//$modx->addPackage('msOrder',$ms2ModelPath , 'modx_'); | |
$data = date('Ymd_Hi'); | |
$id_file = 'id.txt'; // файл в котором хранится последний взятый id | |
$data_file = $data.'_minishoporder.csv'; // файл с выбранными данными | |
// получаем ID последней выбраной записи из файла | |
// если нет файла с последним ID выборки то выбираем все | |
if( file_exists($id_file) && $last_id = file($id_file)){ | |
$where = array( | |
'id:>' => $last_id[0] | |
); | |
}else{ | |
$where = array( | |
'id:>' => 0 | |
); | |
} | |
$q = $modx->newQuery('msOrder'); | |
$q->innerJoin('msOrderAddress', 'msOrderAddress', 'msOrderAddress.id = msOrder.id'); | |
//$q->innerJoin('msOrderProduct', 'msOrderProduct', 'msOrderProduct.order_id = msOrder.id'); | |
$q->where($where); | |
$q->select( array( | |
'msOrder.*', | |
'msOrderAddress.*', | |
/* | |
'msOrder.num', | |
'msOrder.cost', | |
'msOrder.cart_cost', | |
'msOrder.delivery_cost', | |
'msOrderAddress.receiver', | |
'msOrderAddress.phone', | |
'msOrderAddress.country', | |
'msOrderAddress.city', | |
'msOrderProduct*', | |
*/ | |
)); | |
//$q->select(); | |
$q->prepare(); | |
$sql = $q->toSQL(); | |
$query = $modx->prepare($sql); | |
$query->execute(); | |
$orders_arr = $query->fetchAll(PDO::FETCH_ASSOC); | |
if($orders_arr){ | |
$last_order_id = ''; | |
//$orders = array(); | |
//перебираем ордера | |
foreach($orders_arr as $order){ | |
$order_str = implode(';',$order).';'; | |
$need = '/([\r\n])[\s]+/'; | |
$order_str = preg_replace($need, ' ', $order_str); | |
//выбираем товары ордера | |
$order_id = $order['id']; | |
if($last_order_id != $order_id) $last_order_id = $order_id; | |
$q = $modx->newQuery('msOrderProduct'); | |
$where = array( | |
'order_id' => $last_order_id | |
); | |
$q->where($where); | |
$q->select(); | |
$q->prepare(); | |
$sql = $q->toSQL(); | |
$query = $modx->prepare($sql); | |
$query->execute(); | |
$items_arr = $query->fetchAll(PDO::FETCH_ASSOC); | |
//перебираем товары ордера | |
$item_str = ''; | |
$options_str = ''; | |
foreach($items_arr as $item){ | |
$item_str .= 'ID продукта '.$item['msOrderProduct_product_id'].','; | |
$item_str .= 'Наименование продукта '.$item['msOrderProduct_name'].','; | |
$item_str .= 'Количество '.$item['msOrderProduct_count'].','; | |
$item_str .= 'Цена '.$item['msOrderProduct_price'].','; | |
$item_str .= 'Размер '.$item['msOrderProduct_weight'].','; | |
$item_str .= 'Стоимость '.$item['msOrderProduct_cost'].','; | |
$options = json_decode($item['msOrderProduct_options'],$assoc = true); | |
if($options && is_array($options)){ | |
$options_str = implode(',', $options ); | |
}else{ | |
$options_str = ''; | |
} | |
$item_str .= 'Опции '.$options_str.'|'; // '|' разделитель между товарами | |
} | |
$order_str .= $item_str; | |
$order_str_arr[] = $order_str.PHP_EOL; | |
} | |
}; | |
if(isset($order_str_arr) && $order_str_arr){ | |
$bom = "\xEF\xBB\xBF"; | |
file_put_contents($data_file, $bom); | |
if($order_str_arr && file_put_contents($data_file, $order_str_arr,FILE_APPEND) ){ | |
// записываем последний взятый ID если что то брали | |
file_put_contents($id_file, $last_order_id); | |
} | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment