Answer the question
In order to leave comments, you need to log in
How to replace the symbolic code of an element with the article property?
Good afternoon! How to replace the symbolic code of an element with the article property?
I use the code in
AddEventHandler("iblock", "OnAfterIBlockElementAdd", Array("OBEAClass", "OBEA")); // объявляем обработчики
AddEventHandler("iblock", "OnAfterIBlockElementUpdate", Array("OBEAClass", "OBEA"));
class OBEAClass
{
public static $disableHandler = false;
function OBEA(&$arFields)
{
if (self::$disableHandler)
return;
CModule::IncludeModule("iblock");
if( $arFields["ID"]> 0 && $arFields["IBLOCK_ID"] == "33") // ID основного каталога товаров
{
$el = new CIBlockElement;
foreach($arFields["PROPERTY_VALUES"] as $key=>$value)
{
if($key == "CML2_ARTICLE") // ID свойства Артикул (CML2_ARTICLE)
foreach($value as $key2=>$value2)
{
$arLoadProductArray = Array("CODE" => mb_strtolower($value2["VALUE"]));
}
}
self::$disableHandler = true; //отключаем
if($res = $el->Update($arFields["ID"], $arLoadProductArray))
{
}
if($res = $el->Add($arFields["ID"], $arLoadProductArray))
{
}
}
}
}
Answer the question
In order to leave comments, you need to log in
I don’t know how this code looks, good or bad, but only it worked for me. Short, simple and working
CModule::IncludeModule('iblock');
$IBLOCK_ID = 49;
$elDB = CIBlockElement::GetList(
array('SORT' => 'asc'),
array('IBLOCK_ID'=> $IBLOCK_ID),
false,
false,
array('ID', 'NAME', 'CODE', 'PROPERTY_CML2_ARTICLE')
);
while($arEl = $elDB->GetNext(false, false))
{
// echo '<pre>';
// print_r($arEl);
// echo '</pre>';
$arParams = array("replace_space"=>"-","replace_other"=>"-");
$trans = $arEl[PROPERTY_CML2_ARTICLE_VALUE];
// echo '<pre>';
// print_r($trans);
// echo '</pre>';
$el = new CIBlockElement;
$arLoadProductArray = Array(
'CODE' =>$trans
);
$res = $el->Update($arEl['ID'], $arLoadProductArray);
}
Try like this:
<?
AddEventHandler("iblock", "OnAfterIBlockElementAdd", Array("OBEAClass", "OBEA")); // объявляем обработчики
AddEventHandler("iblock", "OnAfterIBlockElementUpdate", Array("OBEAClass", "OBEA"));
class OBEAClass
{
public static $disableHandler = false;
function OBEA(&$arFields)
{
if (self::$disableHandler){return;}
CModule::IncludeModule("iblock");
if( $arFields["ID"]> 0 && $arFields["IBLOCK_ID"] == "33") // ID основного каталога товаров
{
$db_props = CIBlockElement::GetProperty($arFields["IBLOCK_ID"], $arFields["ID"], Array("sort"=>"asc"), Array("CODE"=>"CML2_ARTICLE"));
if($ar_props = $db_props->Fetch()){
$arLoadProductArray = Array("CODE" => mb_strtolower($ar_props["VALUE"]));
if($res = $el->Update($arFields["ID"], $arLoadProductArray)){
}
}
self::$disableHandler = true; //отключаем
}
}
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question