P
P
pavelmosin2019-06-18 21:07:38
1C-Bitrix
pavelmosin, 2019-06-18 21:07:38

Bitrix. Error on the OnBeforeBasketAdd event?

Good afternoon.
I add my properties to the product property when ordering.
I take the properties from another infoblock.
Properties I add on event OnBeforeBasketAdd
That I want to make.
Get all infoblock elements with properties and check if they are filled in when sending or not.
Why doesn't this code work?

<?
  use Bitrix\Main\Context;


  if (!CModule::IncludeModule('iblock')) return;


  AddEventHandler(
    "sale",
    "OnBeforeBasketAdd",
    'addProperty'
  );



  function addProperty(&$arFields) {


    $request = Context::getCurrent()->getRequest();


    // Выбираем опции
    $arOrder = Array(
      "SORT"=>"ASC"
    );


    $arSelect = Array(
      "ID",
      "IBLOCK_ID",
      "CODE",
      "NAME",
      "PROPERTY_OPTION_TYPE",
    );


    $arFilter = Array(
      "IBLOCK_ID" => '2',
    );


    $res = CIBlockElement::GetList(
      $arOrder,
      $arFilter,
      false,
      false,
      $arSelect
    );


    while( $ob = $res->GetNextElement() ) {
      $arFields[] = $ob->GetFields();
    }


    $arOptionsAddToBasket['OPTIONS'] = $arFields;




    // Заполняем опции
    foreach ( $arOptionsAddToBasket['OPTIONS'] as $value ) {


      $arFields['PROPS'][$value['CODE']] = [
        'CODE' => $value['CODE'],
        'ID' => '',
        'VALUE' => '1',
        'SORT' => '',
        'NAME' => $value['NAME'],
      ];
    }
  }
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2019-06-19
@pavelmosin

while( $ob = $res->GetNextElement() ) {
      $arFields[] = $ob->GetFields();//Тут ошибка. эта переменная используется в качестве параметра функции addProperty. 
}

It should be like this:
function addProperty(&$arFields)
{

    // Выбираем опции
    $arOrder = Array(
        "SORT" => "ASC"
    );


    $arSelect = Array(
        "ID",
        "IBLOCK_ID",
        "CODE",
        "NAME",
        "PROPERTY_OPTION_TYPE",
    );


    $arFilter = Array(
        "IBLOCK_ID" => '19',
    );


    $res = CIBlockElement::GetList(
        $arOrder,
        $arFilter,
        false,
        false,
        $arSelect
    );

    $counter = 0;
    while ($ob = $res->GetNextElement()) {
        $arItem = $ob->GetFields();
        $arFields['PROPS'][$arItem['CODE']] = [
            'CODE' => $arItem['CODE'],
            'VALUE' => '1',
            'SORT' => $counter += 100,//Про сортировку свойств не надо забывать. ID был лишним.
            'NAME' => $arItem['NAME'],
        ];
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question