Skip to content

Instantly share code, notes, and snippets.

@EscApp2
Last active April 17, 2024 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EscApp2/bcdd0556343f3c9e5ce25fa141e23557 to your computer and use it in GitHub Desktop.
Save EscApp2/bcdd0556343f3c9e5ce25fa141e23557 to your computer and use it in GitHub Desktop.
selected sku by filter, выбрать ID SKU для установки SKU по умолчанию, исходя из умного фильтра.
<?
/*
* выбрать ID SKU для установки SKU по умолчанию, исходя из умного фильтра.
* */
function getOffersSelectedByFilter(&$arResult, &$arParams, $component){
//https://pai-bx.com/wiki/1c-bitrix/2357-filtered-sku-in-products-list/
if(!empty($arParams['FILTER_NAME'])){
global ${$arParams['FILTER_NAME']};
$FILTER = ${$arParams['FILTER_NAME']};
if(!empty($FILTER) && !empty($FILTER['OFFERS']) ) {
foreach ($arResult['ITEMS'] as &$arItem){
$arFiltredOffers = [];
foreach ($FILTER['OFFERS'] as $filterKey=>$arFilterValues)
{
$arFiltredOffers[$filterKey] = array();
$arFilterKey = explode('_', $filterKey);
$filterPropertyId = array_pop($arFilterKey);
$filter_condition = substr($filterKey, 0, 1);
foreach ($arItem['OFFERS'] as $arOffer){
$returnOffer = false;
foreach ($arOffer['PROPERTIES'] as $arProperty){
if($arProperty['ID']==$filterPropertyId){
if($filter_condition=='='){
// work with property type = S and L
foreach ($arFilterValues as $filterValue){
$VALUE = $arProperty['VALUE'];
if(
isset($arProperty['VALUE_ENUM_ID'])
&& !empty($arProperty['VALUE_ENUM_ID'])
){
$VALUE = $arProperty['VALUE_ENUM_ID'];
}
if(is_array($VALUE)){
if(in_array($filterValue,$VALUE)){
$returnOffer = true;
}
}else{
if($filterValue==$VALUE){
$returnOffer = true;
}
}
}
}elseif($filter_condition=='>'){ // ><
// TODO check
$MIN = $arFilterValues[0];
$MAX = $arFilterValues[1];
$VALUE = $arProperty['VALUE'];
//TODO multiple VALUE
if($VALUE>$MIN && $VALUE<$MAX){
$returnOffer = true;
}
}
}
}
if($returnOffer){
$arFiltredOffers[$filterKey][] = $arOffer['ID'];
}
}
}
$arFiltredOffers = array_values($arFiltredOffers);
$arAvailOffers = call_user_func_array('array_intersect', $arFiltredOffers);
$arSelectedOffer = false;
foreach($arItem['OFFERS'] as $arOffer){
if(in_array($arOffer['ID'],$arAvailOffers)){
$arSelectedOffer = $arOffer;
break;
}
}
if($arSelectedOffer){
$arItem['SELECTED_OFFER_BY_FILTER'] = array(
"ID"=>$arSelectedOffer['ID'],
"CODE"=>$arSelectedOffer['CODE'],
);
$arResult['SELECTED_OFFER_BY_FILTER'][$arItem['ID']] = $arItem['SELECTED_OFFER_BY_FILTER'];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment