Last active
January 16, 2020 06:44
-
-
Save SeRGei93/cfa0727814d21320863bc1d1c6c28159 to your computer and use it in GitHub Desktop.
Битрикс получить товары со скидкой
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
function getAllDiscounts() | |
{ | |
Bitrix\Main\Loader::includeModule('sale'); | |
require_once ($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/sale/handlers/discountpreset/simpleproduct.php"); | |
$arDiscounts = array(); | |
$arProductDiscountsObject = \Bitrix\Sale\Internals\DiscountTable::getList(array( | |
'filter' => array( | |
'=ACTIVE' => 'Y', | |
// 'ID' => 1231, | |
), | |
'select' => array( | |
"*" | |
) | |
)); | |
while( $arProductDiscounts = $arProductDiscountsObject->fetch() ){ | |
$discountObj = new Sale\Handlers\DiscountPreset\SimpleProduct(); | |
$discount = $discountObj->generateState($arProductDiscounts); | |
array_push($arDiscounts, array( | |
"PRODUCTS" => $discount['discount_product'], | |
"TYPE" => $discount['discount_type'], | |
"SECTIONS" => $discount['discount_section'], | |
"VALUE" => $discount['discount_value'], | |
)); | |
} | |
return $arDiscounts; | |
} | |
function getDiscountProducts(){ | |
$arDiscounts = getAllDiscounts(); | |
$out = array( | |
"PRODUCTS" => array(), | |
#"SECTIONS" => array() | |
); | |
$sections = array(); | |
foreach ($arDiscounts as $key => $arDiscount) { | |
// if( isset( $arDiscount["PRODUCTS"] ) && count($arDiscount["PRODUCTS"]) ){ | |
// $out["PRODUCTS"] = array_merge($out["PRODUCTS"], $arDiscount["PRODUCTS"]); | |
// } | |
// if( isset( $arDiscount["SECTIONS"] ) && count($arDiscount["SECTIONS"]) ){ | |
// $out["SECTIONS"] = array_merge($out["SECTIONS"], $arDiscount["SECTIONS"]); | |
// } | |
if( isset( $arDiscount["PRODUCTS"] ) && count($arDiscount["PRODUCTS"]) ){ | |
$out["PRODUCTS"] = array_merge($out["PRODUCTS"], $arDiscount["PRODUCTS"]); | |
} | |
} | |
return $out["PRODUCTS"]; | |
} | |
function setElementGroups($elementId) | |
{ | |
$sectSale = 1103; // ID раздела скидок | |
$ar_new_groups = []; | |
$db_old_groups = CIBlockElement::GetElementGroups($elementId, true); | |
while($ar_group = $db_old_groups->Fetch()){ | |
if ($ar_group["ID"] != $sectSale){ | |
$ar_new_groups[] = $ar_group["ID"]; | |
} | |
} | |
CIBlockElement::SetElementSection($elementId, $ar_new_groups); | |
} | |
$_SERVER["DOCUMENT_ROOT"] = "/var/www/bitrix/data/www/cookhouse.by"; | |
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"]; | |
require_once($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php"); | |
$i = 0; | |
CModule::IncludeModule('iblock'); | |
$sectSale = 1103; | |
$arSelect = Array("ID", "IBLOCK_SECTION_ID"); | |
$arFilter = Array("IBLOCK_ID" => 17, "ACTIVE"=>"Y", "SECTION_ID" => $sectSale); | |
$res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); | |
while($ob = $res->GetNextElement()) | |
{ | |
$arFields = $ob->GetFields(); | |
setElementGroups($arFields['ID'], true); | |
} | |
$arSelect = Array("ID"); | |
$arFilter = Array("IBLOCK_ID" => 17, "ACTIVE"=>"Y", "ID" => getDiscountProducts()); | |
$res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); | |
while($ob = $res->GetNextElement()) | |
{ | |
$arFields = $ob->GetFields(); | |
$ar_new_groups = []; | |
$db_old_groups = CIBlockElement::GetElementGroups($arFields['ID'], true); | |
while($ar_group = $db_old_groups->Fetch()){ | |
//echo $ar_group["ID"] . '<br>'; | |
$ar_new_groups[] = $ar_group["ID"]; | |
} | |
$ar_new_groups[] = $sectSale; | |
//dump($ar_new_groups); | |
CIBlockElement::SetElementSection($arFields['ID'], $ar_new_groups); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment