D
D
daylight62021-05-12 13:11:56
1C-Bitrix
daylight6, 2021-05-12 13:11:56

How to bind an infoblock element to another section when it is changed?

Hello.

When I change an element, I'm trying to make it automatically bind in addition to one more section.
I can't figure out what's wrong, please tell me:

AddEventHandler("iblock", "OnAfterIBlockElementUpdate", Array("AddToSection", "OnAfterIBlockElementUpdateHandler"));

class AddToSection 
{
  public static $disableHandler = false;
  
  function OnAfterIBlockElementUpdateHandler(&$arFields)
  {
    if (self::$disableHandler)
      return;

    CModule::IncludeModule("iblock");

    if( $arFields["ID"] > 0 && $arFields["IBLOCK_ID"] == "21")
    {
      $el = new CIBlockElement;

      $arSections = array();

      if(in_array("104", $arFields["IBLOCK_SECTION"], true))
      {
        file_put_contents("/home/bitrix/www/bitrix/php_interface/log.txt", "Товар уже находится в папке 104");
      }
      else 
      {
        $arSections = array_push($arFields["IBLOCK_SECTION"], "104");

        $arLoadProductArray = Array(
          "IBLOCK_SECTION" => $arSections,
        );

        self::$disableHandler = true;

        $res = $el->Update($arFields["ID"], $arLoadProductArray);
      }
    }
  }
}


Binding to any section generally disappears in this case. And for some reason, $arSections displays not an array of sections, but the number 2...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question