Answer the question
In order to leave comments, you need to log in
How to automatically add a symbolic code to all files in Bitrix?
How to automatically add a symbolic code to all files in Bitrix?
The purpose of the symbolic code: to form a url for the product, without a symbolic code I have an empty address on all products where there is no symbolic code
Created a file in the php_interface folder called init.php
Added this code:
AddEventHandler("iblock", "OnBeforeIBlockElementAdd", "addElement");
AddEventHandler("iblock", "OnBeforeIBlockElementUpdate", array("MyEventHandlerClass", "IBlockElementAddCodeFromName"));
function addElement($arFields){
$arParams = array(
"max_len" => "60", // обрезаем символьный код до 60 символов
"change_case" => "L", // приводим к нижнему регистру
"replace_space" => "-", // меняем пробелы на тире
"replace_other" => "-", // мен¤ем плохие символы на тире
"delete_repeat_replace" => "true", // удаляем повтор¤ющиеся тире
"use_google" => "false", // отключаем использование google
);
$arFields["CODE"] = Cutil::translit($arFields["NAME"], "ru", $arParams);
}
Answer the question
In order to leave comments, you need to log in
I didn't fully understand what you want to do.
you have set a handler for the element addition event, although this can be configured in the infoblock.
the second handler will not work for you.
If you add a symbolic code to already existing products, then
here is an example code. the bottom line is that you need to make a selection, translate the name, save
Here and sections and elements
<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
CModule::IncludeModule('iblock');
$IBLOCK_ID = 3;
$fileElement = $_SERVER['DOCUMENT_ROOT'].'/scripts/elements.txt';
$fileSection = $_SERVER['DOCUMENT_ROOT'].'/scripts/section.txt';
$rsSections = CIBlockSection::GetList(
array('SORT' => 'asc'),
array('IBLOCK_ID' => $IBLOCK_ID),
false,
array('ID', 'NAME', 'CODE'),
false
);
$currentSect = file_get_contents($fileSection);
$currentSect .= "-----------------\r\n";
while ($arSction = $rsSections->GetNext(false, false))
{
echo '<pre>';
print_r($arSction);
echo '</pre>';
$arParamsSect = array("replace_space"=>"-","replace_other"=>"-");
$transSect = Cutil::translit($arSction['NAME'],"ru",$arParamsSect);
echo '<pre>';
print_r($transSect);
echo '</pre>';
$currentSect .= $arSction['CODE'].' - '.$transSect."\r\n";
file_put_contents($fileSection, $currentSect);
$bs = new CIBlockSection;
$arFields = Array(
'CODE' => $transSect
);
$res = $bs->Update($arSction['ID'], $arFields);
}
$elDB = CIBlockElement::GetList(
array('SORT' => 'asc'),
array('IBLOCK_ID'=> $IBLOCK_ID),
false,
false,
array('ID', 'NAME', 'CODE')
);
$currentEl = file_get_contents($fileElement);
$currentEl .= "-----------------\r\n";
while($arEl = $elDB->GetNext(false, false))
{
echo '<pre>';
print_r($arEl);
echo '</pre>';
$arParams = array("replace_space"=>"-","replace_other"=>"-");
$trans = Cutil::translit($arEl['NAME'],"ru",$arParams);
echo '<pre>';
print_r($trans);
echo '</pre>';
print_r($currentEl);
$currentEl .= $arEl['CODE'].' - '.$trans."\r\n";
file_put_contents($fileElement, $currentEl);
$el = new CIBlockElement;
$arLoadProductArray = Array(
'CODE' =>$trans
);
$res = $el->Update($arEl['ID'], $arLoadProductArray);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question