{
//Сущность элементов инфоблока
$iblockEntity = \Bitrix\Iblock\IblockTable::compileEntity('catalogApi');
$this->iblockEntity = $iblockEntity;
//Сущность товаров
$productEntity = \Bitrix\Main\ORM\Entity::compileEntity(
'CATALOG_PRODUCTS',
[
(new \Bitrix\Main\ORM\Fields\IntegerField('ID'))->configurePrimary(),
(new \Bitrix\Main\ORM\Fields\IntegerField('QUANTITY')),
(new \Bitrix\Main\ORM\Fields\StringField('AVAILABLE')),
],
[
'namespace' => 'idea',
'table_name' => 'b_catalog_product',
]
);
$this->productEntity = $productEntity;
$this->prodId = $prodId;
}
public function GetProducts():array
{
$iblockEntity = $this->iblockEntity;
//Добавим поля товара к элементам
$iblockEntity->addField(new \Bitrix\Main\Entity\ReferenceField(
'PRODUCT',
$this->productEntity,
\Bitrix\Main\ORM\Query\Join::on('this.ID', 'ref.ID')
));
//Получим все элементы
$arProducts = [];
if($this->prodId > 0){
$ObjProductEntity = (new \Bitrix\Main\ORM\Query\Query($iblockEntity))
->setFilter(['ID' => $this->prodId])//Один элемент
->setOrder(['QUANTITY' => 'ASC'])
->setSelect(['ID', 'NAME', 'ACTIVE', 'IBLOCK_SECTION_ID', 'QUANTITY' =>'PRODUCT.QUANTITY'])
->exec();
}else{
$ObjProductEntity = (new \Bitrix\Main\ORM\Query\Query($iblockEntity))
//->setFilter(['IBLOCK_SECTION_ID' => '514'])//Тестирование на одном разделе
->setOrder(['QUANTITY' => 'ASC'])
->setSelect(['ID', 'NAME', 'ACTIVE', 'IBLOCK_SECTION_ID', 'QUANTITY' =>'PRODUCT.QUANTITY'])
->exec();
}
$AllProducts = [];
while ($catObg = $ObjProductEntity->fetch()){
$AllProducts[] = $catObg;
}
return $AllProducts;
}
//Действия по активации/деактивации
public function RunActionElements()
{
foreach($this->GetProducts() as $product)
{
if($product['QUANTITY'] <= 0 && $product['ACTIVE'] == 'Y')
{
$el = new CIBlockElement;
$el->Update($product['ID'], ['ACTIVE' => 'N']);
}elseif($product['QUANTITY'] > 0 && $product['ACTIVE'] == 'N'){
$el = new CIBlockElement;
$el->Update($product['ID'], ['ACTIVE' => 'Y']);
}
}
}
public function GetSectionsTree()
{
$AllProducts = $this->GetProducts();
$AllSections = [];
foreach($AllProducts as $products)
{
$AllSections[] = $products['IBLOCK_SECTION_ID'];
}
$AllSections = array_unique($AllSections);
$AllSections = array_values($AllSections);
//Сборка разделов с элементами
$res = [];
foreach($AllSections as $key => $section)
{
foreach($AllProducts as $products)
{
if($section == $products['IBLOCK_SECTION_ID']){
$res[$AllSections[$key]][] = [
'ID' => $products['ID'],
'ACTIVE' => $products['ACTIVE'],
'SECTION_ID' => $products['IBLOCK_SECTION_ID']
];
}
}
}
unset($AllSections);
return $res;
}
//Действия по активации/деактивации разделов
public function RunActionSections()
{
$tree = $this->GetSectionsTree();
foreach($tree as $key => $section)
{
$count = count($section);
$countNoActive = 0;
foreach($section as $item)
{
if($item['ACTIVE'] == 'N')
{
$countNoActive++;
}
}
if($count == $countNoActive)
{
$bs = new CIBlockSection;
$res = $bs->Update($item['SECTION_ID'], ['ACTIVE' => 'N']);
if(!$res){
AddMessage2Log("Deactivate Error: Раздел ". $item['SECTION_ID'] . $bs->LAST_ERROR);
}
}else
{
$bs = new CIBlockSection;
ну во-первых у вас зацикливается событие)
Обсуждают сегодня