Май 30, 2023
Теги: Магазин
Добавление скидки с помощью классов "\Bitrix\Sale\Internals\DiscountTable", "\Bitrix\Sale\Internals\DiscountGroupTable".
Добавление пятипроцентной скидки на товары инфоблока с идентификатором 15, при наличии определенных значений в свойстве с символьным кодом "tags":
\Bitrix\Main\Loader('iblock');
\Bitrix\Main\Loader('sale');
$IBLOCK_CATALOG_ID = 15;
$name = 'Test discount';
$siteId = 's1';
$userGroupIds = array();
$groupIterator = \Bitrix\Main\GroupTable::getList(array(
'select' => array('ID'),
));
while($group = $groupIterator->fetch())
{
$userGroupIds[] = $group['ID'];
}
$propertyTagConditions = array();
$rsProperty = \Bitrix\Iblock\PropertyTable::getList(array(
'filter' => array('IBLOCK_ID'=>$IBLOCK_CATALOG_ID,'=CODE'=>'tags'),
'select' => array('ID','CODE'),
));
while($property=$rsProperty->fetch())
{
$rsEnum = \Bitrix\Iblock\PropertyEnumerationTable::getList(array(
'filter' => array('PROPERTY_ID'=>$property['ID']),
));
while($enum=$rsEnum->fetch())
{
if(!in_array($enum["XML_ID"],array('sale','rasprodazha','aktsiya','skidka')))
continue;
$propertyTagConditions[] = array (
'CLASS_ID' => 'CondIBProp:'.$IBLOCK_CATALOG_ID.':'.$property['ID'],
'DATA' =>
array (
'logic' => 'Equal',
'value' => $enum["ID"],
),
)
}
}
$discountFields = array(
'LID' => $siteId,
'NAME' => $name,
'ACTIVE_FROM' => '',
'ACTIVE_TO' => '',
'ACTIVE' => 'Y',
'SORT' => '100',
'PRIORITY' => '1',
'LAST_DISCOUNT' => 'Y',
'XML_ID' => '',
'ACTIONS_LIST' => array (
'CLASS_ID' => 'CondGroup',
'DATA' => array (
'All' => 'AND',
),
'CHILDREN' => array(
array(
'CLASS_ID' => 'ActSaleBsktGrp',
'DATA' => array (
'Type' => 'Discount',
'Value' => 5.0,
'Unit' => 'Perc',
'Max' => 0,
'All' => 'OR',
'True' => 'True',
),
'CHILDREN' => $propertyTagConditions,
),
),
),
);
$resultDiscountAdd = \Bitrix\Sale\Internals\DiscountTable::add($discountFields);
if($resultDiscountAdd->isSuccess())
{
foreach($userGroupIds as $groupId)
{
\Bitrix\Sale\Internals\DiscountGroupTable::add(array(
'ACTIVE' => 'Y',
'GROUP_ID' => $groupId,
'DISCOUNT_ID' => $resultDiscountAdd->getId(),
));
}
}