Обновление свойств заказов
Скрипт проходит по всем заказам, выбирает пользователя, и на основании его привязки к городу обновляет свойство заказа «страна».
- orders-update.php
if(php_sapi_name() !== 'cli') die('Access denied'); // только по расписанию из CLI $_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__); define('LANGUAGE_ID', 's1'); define('NO_KEEP_STATISTIC', true); define('NOT_CHECK_PERMISSIONS', true); require($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'); class OrdersUpdater { private $propertyId = 10; // код свойства для обновления public static $countries = array( // маппинг кодов 26 => 'Россия', 8 => 'Казахстан', 1 => 'Белоруссия', 105 => 'Украина' ); public function __construct() { if(!(CModule::IncludeModule('sale') && CModule::IncludeModule('iblock'))) throw new Exception('Необходимы модули магазина и инфоблок', 1); } public function run() { $orderList = CSaleOrder::GetList(array('ID'=>'DESC'), $arFilter = array(), false, false, array('ID')); while ($order = $orderList->Fetch()) { try { $orderId = $order['ID']; $this->updateOrder($orderId); } catch (Exception $e) { fwrite(STDERR, $e->getMessage() . PHP_EOL); } echo 'Done: ' . $orderId . PHP_EOL; } } private function updateOrder($orderId) { $orderList = CSaleOrder::GetList(array('ID'=>'DESC'), $arFilter = array('ID' => $orderId), false, false, array('ID', 'USER_ID')); if($orderList->SelectedRowsCount() != 1) throw new Exception('No such order: ' . $orderId, 1); $order = $orderList->Fetch(); $userList = CUser::GetByID($order['USER_ID']); if($userList->SelectedRowsCount() != 1) throw new Exception('No such user: ' . $order['USER_ID'], 1); $user = $userList->Fetch(); $countryName = $this->getCountryName($user); $this->setProperty($orderId, $countryName); } private function setProperty($orderId, $value) { $propsList = CSaleOrderPropsValue::GetList('ID', 'ASC', array('ORDER_ID' => $orderId, 'ORDER_PROPS_ID' => $this->propertyId)); if($propsList->SelectedRowsCount() == 1) { $prop = $propsList->Fetch(); $arFields = array('VALUE' => $value); $ret = CSaleOrderPropsValue::Update($prop['ID'], $arFields); if(!$ret) throw new Exception('Can not update order: ' . $orderId, 1); } else { $ret = CSaleOrderPropsValue::Add(array( 'ORDER_ID' => $orderId, 'ORDER_PROPS_ID' => $this->propertyId, 'NAME' => 'Страна', 'CODE' => 'COUNTRY', 'VALUE' => $value, )); if(!$ret) throw new Exception('Can not add property to order: ' . $orderId, 1); } } private function getCountryName($user) { $arFilter = array('IBLOCK_ID' => COUNTRY_IBLOCK_ID, 'ID' => $user['UF_CITY'], 'INCLUDE_SUBSECTIONS' => 'Y'); $locationList = CIBlockElement::GetList(array(), $arFilter, false, false, array('ID', 'IBLOCK_SECTION_ID', 'IBLOCK_ID', 'NAME')); if($locationList->SelectedRowsCount() != 1) throw new Exception('No such location: ' . $user['UF_CITY'], 1); $location = $locationList->Fetch(); $treeList = CIBlockSection::GetNavChain(COUNTRY_IBLOCK_ID, $location['IBLOCK_SECTION_ID']); $level1 = $treeList->Fetch(); if(array_key_exists($level1['ID'], self::$countries)) return $level1['NAME']; throw new Exception("No country: " . $level1['ID'], 1); } } $OrdersUpdater = new OrdersUpdater(); $OrdersUpdater->run();