AddEventHandler("catalog", "OnBeforeCatalogImport1C", "CatalogWeightProcessor::OnBeforeCatalogImport1C");
AddEventHandler("catalog", "OnSuccessCatalogImport1C", "CatalogWeightProcessor::OnSuccessCatalogImport1C");
class CatalogWeightProcessor
{
protected static $exchangeStart = null;
public static function OnBeforeCatalogImport1C()
{
// запоминаем время начала обмена, чтобы вытащить только обновленные товары
self::$exchangeStart = time();
}
public static function OnSuccessCatalogImport1C()
{
Bitrix\Main\Loader::includeModule('iblock');
$select = [
'ID',
'PROPERTY_[вес]',
];
$filter = [
'=IBLOCK_ID' => "ид каталога",
'>=TIMESTAMP_X' => Bitrix\Main\Type\DateTime::createFromTimestamp(self::$exchangeStart),
];
$result = CIBlockElement::GetList([], $filter, false, false, $select);
while ($row = $result->GetNext()) {
$productId = $row['ID'];
$weight = (float) $row['PROPERTY_[вес]_VALUE'];
$result = Bitrix\Catalog\ProductTable::update($productId, [
'WEIGHT' => $weight,
]);
if (!$result->isSuccess()) {
$errors = $result->getErrorMessages();
CEventLog::Add([
'SEVERITY' => 'ERROR',
'AUDIT_TYPE_ID' => 'exchange1c',
'MODULE_ID' => 'catalog',
'ITEM_ID' => $productId,
'DESCRIPTION' => join(",", $errors),
]);
}
}
}
}