Created
October 10, 2017 12:34
-
-
Save Pum-purum/f85779d22095340387533a2fa469117f to your computer and use it in GitHub Desktop.
Обновление количества товаров в Каталоге из файла *.xlsx. Требует модуль nkhost.phpexcel
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 | |
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/header.php"); | |
$filepath = $_SERVER['DOCUMENT_ROOT'] . "/upload/price.xlsx"; //файл для считывания | |
$beginIndex = 12; //первая строка, с которой нужно начать считывание | |
define("IBLOCK_CATALOG_ID", 4); //ID инфоблока с каталогом | |
define("SUPPLIER_CODE", "OLDIMGROUP"); //код свойства с поставщиком | |
if (!file_exists($filepath)) { | |
print "No input file specified.\n"; | |
return; | |
} else { | |
\CModule::includeModule('iblock'); | |
\CModule::includeModule('catalog'); | |
\CModule::IncludeModule('nkhost.phpexcel'); | |
global $PHPEXCELPATH; | |
require_once($PHPEXCELPATH . '/PHPExcel/IOFactory.php'); | |
$oExcel = PHPExcel_IOFactory::load($filepath); | |
$i = $beginIndex; | |
$notEmptyValues = 0; | |
while ($oExcel->getActiveSheet()->getCell('B' . $i)->getValue() !== NULL) { | |
$Ccell = $oExcel->getActiveSheet()->getCell('C' . $i)->getValue(); | |
if ($Ccell == NULL) { | |
$i++; | |
continue; | |
} | |
$article = $oExcel->getActiveSheet()->getCell('B' . $i)->getValue(); | |
$article = trim($article); | |
$rawQuantity = $oExcel->getActiveSheet()->getCell('F' . $i)->getValue(); | |
$rsElements = \CIBlockElement::GetList( | |
[], | |
[ | |
'IBLOCK_ID' => IBLOCK_CATALOG_ID, | |
'PROPERTY_CML2_ARTICLE' => $article, | |
[ | |
"LOGIC" => "OR", | |
["=PROPERTY_SUPPLIER" => false], | |
["=PROPERTY_SUPPLIER" => SUPPLIER_CODE] | |
], | |
], | |
false, | |
false, | |
['ID', 'NAME', 'PROPERTY_CML2_ARTICLE', 'PROPERTY_SUPPLIER'] | |
); | |
if ($rsElements->SelectedRowsCount() == 1) { | |
$arElement = $rsElements->Fetch(); | |
if ($rawQuantity == '>10') { | |
$quantity = 11; | |
} elseif ($rawQuantity == '<=10') { | |
$quantity = 5; | |
} else { | |
$quantity = 0; | |
} | |
$updateCatalog = \CCatalogProduct::Update( | |
$arElement['ID'], | |
[ | |
'QUANTITY' => $quantity, | |
'QUANTITY_TRACE' => 'Y' | |
] | |
); | |
if ($updateCatalog) { | |
$notEmptyValues++; | |
echo "#" . $arElement['ID'] . " " . $arElement['NAME'] . " установлено количество " . $quantity . " шт."; | |
echo "<br>"; | |
} else { | |
echo "#" . $arElement['ID'] . " " . $arElement['NAME'] . " не удалось обновить количество."; | |
echo "<br>"; | |
} | |
\CIBlockElement::SetPropertyValuesEx( | |
$arElement['ID'], | |
IBLOCK_CATALOG_ID, | |
['SUPPLIER' => SUPPLIER_CODE] | |
); | |
\Bitrix\Iblock\PropertyIndex\Manager::updateElementIndex( | |
IBLOCK_CATALOG_ID, | |
$arElement['ID'] | |
); | |
} | |
$i++; | |
} | |
$i--; | |
$count = $i - $beginIndex + 1; | |
echo "<br>Обновлено количество в " . $notEmptyValues . " товарах, обработаны строки с {$beginIndex} по {$i}, всего {$count} строк"; | |
} | |
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/footer.php"); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment