S
S
symply_rabbit2019-06-05 17:05:52
1C-Bitrix
symply_rabbit, 2019-06-05 17:05:52

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))
                    {
                    }
 
        }
         
    }
}

But nothing happens, as there was a symbolic code in Latin from the name, and nothing happened.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
symply_rabbit, 2019-06-06
@symply_rabbit

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);
    }

A
Alams Stoyne, 2019-06-05
@alams_stoyne

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 question

Ask a Question

731 491 924 answers to any question