if CModule IncludeModule catalog ID 777 ID товара NAME Плюшка подушка

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?
if( CModule::IncludeModule( "catalog" ) ) {
$ID = 777; //ID товара
$NAME = "Плюшка подушка"; //имя товара
$SITE = "s1"; //id сайта
$NEWPrice = 100; // цена со скидкой
//Получим все скидки товара
$arDiscounts = CCatalogDiscount::GetList(
[ "SORT" => "ASC" ],
[
"+PRODUCT_ID" => (int)$ID,
"SITE_ID" => $SITE,
],
FALSE,
FALSE,
[
"ID", "ACTIVE",
]
);
$haveDiscounts = $arDiscounts->SelectedRowsCount() > 0;
$arFields = [
"SITE_ID" => $SITE,
"ACTIVE" => "Y",
"LAST_DISCOUNT" => "Y",
"NAME" => $NAME,
"COUPON" => "",
"SORT" => "1",
"CATALOG_GROUP_IDS " => [ 2 ],
"VALUE_TYPE" => "S", //тип скидки (P - в процентах, F - фиксированная величина, S - фиксированная цена);
"VALUE" => $NEWPrice,
"CURRENCY" => "RUB",
"RENEWAL" => "N",
"CONDITIONS" => [
"CLASS_ID" => "CondGroup",
"DATA" => [
"All" => "AND",
"True" => "True",
],
"CHILDREN" => [
[ "CLASS_ID" => "CondIBElement",
"DATA" => [
"logic" => "Equal",
"value" => [ $ID ],
],
],
],
],
];
if( $haveDiscounts ) {
//Скидки уже есть. Обновим только первую остальные удалим.
$first = TRUE;
while( $discount = $arDiscounts->Fetch() ) {
if( $first ) {
$first = FALSE;
CCatalogDiscount::Update( (int)$discount["ID"], $arFields );
} else {
CCatalogDiscount::Delete( (int)$discount["ID"] );
}
}
} else {
//Скидок нет. Создадим новую
$ID = CCatalogDiscount::Add( $arFields );
$res = $ID > 0;
if( !$res ) {
$ex = $APPLICATION->GetException();
}
}
}
?>