Skip to content

Instantly share code, notes, and snippets.

@EscApp2
Created August 18, 2016 13:52
Show Gist options
  • Save EscApp2/83c036d1e40f6aeb287f2b532935df2b to your computer and use it in GitHub Desktop.
Save EscApp2/83c036d1e40f6aeb287f2b532935df2b to your computer and use it in GitHub Desktop.
bitrix catalog calc
<?
class nav_catalog{
function canBuy($arItem){
if(isset($arItem['OFFERS']) && !empty($arItem['OFFERS'])){
$can_buys = array();
foreach($arItem['OFFERS'] as $offer){
$can_buys[] = self::canBuy($offer);
}
$can_buy = "N";
foreach($can_buys as $one_can_buy){
if($one_can_buy == "Y"){
$can_buy = "Y";
break;
}
}
return $can_buy;
}elseif(isset($arItem['OFFERS']) && empty($arItem['OFFERS'])){
return false;
}else{
foreach($arItem['PRICES'] as $price){
break;
}
return $price['CAN_BUY'];
}
}
function minPrice($arItem, $discount = true ){
if(isset($arItem['OFFERS']) && !empty($arItem['OFFERS'])){
$prices = array();
foreach($arItem['OFFERS'] as $offer){
$prices[] = self::minPrice($offer); //no discount flag because no offers in offers
}
//pre($prices);
$minPrice = $prices[0]['VALUE'];
$minPriceKey = 0;
foreach($prices as $key=>$price){
$value = $price['VALUE'];
if($discount){
if($price['DISCOUNT_VALUE'] < $value){
$value = $price['DISCOUNT_VALUE'];
}
}
if($minPrice > $value){
$minPrice = $value;
$minPriceKey = $key;
}
}
return $prices[$minPriceKey];
}else{
foreach($arItem['PRICES'] as $price){
break;
}
return $price;
}
}
function minPriceEx($arElement){
$arMinPrice = array();
if(is_array($arElement["OFFERS"]) && !empty($arElement["OFFERS"])) //Product has offers
{
$minItemPrice = 0;
$minItemPriceFormat = "";
foreach($arElement["OFFERS"] as $arOffer)
{
foreach($arOffer["PRICES"] as $code=>$arPrice)
{
if($arPrice["CAN_ACCESS"])
{
if ($arPrice["DISCOUNT_VALUE"] < $arPrice["VALUE"])
{
$minOfferPrice = $arPrice["DISCOUNT_VALUE"];
$minOfferPriceFormat = $arPrice["PRINT_DISCOUNT_VALUE"];
}
else
{
$minOfferPrice = $arPrice["VALUE"];
$minOfferPriceFormat = $arPrice["PRINT_VALUE"];
}
if ($minItemPrice > 0 && $minOfferPrice < $minItemPrice)
{
$minItemPrice = $minOfferPrice;
$minItemPriceFormat = $minOfferPriceFormat;
$arMinPrice = $arPrice;
}
elseif ($minItemPrice == 0) //for first step
{
$minItemPrice = $minOfferPrice;
$minItemPriceFormat = $minOfferPriceFormat;
$arMinPrice = $arPrice;
}
}
}
}
if ($minItemPrice > 0)
{
$arElement["MIN_OFFER_PRICE"] = $minItemPrice;
$arElement["PRINT_MIN_OFFER_PRICE"] = $minItemPriceFormat;
}
}else{
$minItemPrice = 0;
$minItemPriceFormat = "";
foreach($arElement["PRICES"] as $code=>$arPrice){
if($arPrice["CAN_ACCESS"]){
if ($arPrice["DISCOUNT_VALUE"] < $arPrice["VALUE"]){
$minOfferPrice = $arPrice["DISCOUNT_VALUE"];
$minOfferPriceFormat = $arPrice["PRINT_DISCOUNT_VALUE"];
}else{
$minOfferPrice = $arPrice["VALUE"];
$minOfferPriceFormat = $arPrice["PRINT_VALUE"];
}
if ($minItemPrice > 0 && $minOfferPrice < $minItemPrice)
{
$minItemPrice = $minOfferPrice;
$minItemPriceFormat = $minOfferPriceFormat;
$arMinPrice = $arPrice;
}
elseif ($minItemPrice == 0) //for first step
{
$minItemPrice = $minOfferPrice;
$minItemPriceFormat = $minOfferPriceFormat;
$arMinPrice = $arPrice;
}
}
}
}
return $arMinPrice;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment