+7 495 008 8452 пн.-пт. 10:00 – 17:00
Загрузка...
Если у вас возникли какие либо вопросы которые вы не смогли решить по нашим публикациям самостоятельно,
то ждем ваше обращение в нашей службе тех поддержки.


Динамические скидки в торговом каталоге

Пример кастомизации стандартного расчета скидок

вариант 1
AddEventHandler("catalog", "OnGetDiscount", array("artDiscount", "GetDiscount"));
CModule::IncludeModule('catalog');

вариант 2 (спасибо, Жуков Евгений )
AddEventHandler("catalog", "OnGetDiscountResult", array("artDiscount", "OnGetDiscountResult"));
AddEventHandler("catalog", "OnGetDiscount", array("artDiscount", "OnGetDiscount"));



Вариант 2 (рекомендуемый)

class artDiscount {
   
   static private $pId = false;
   
   public function OnGetDiscount($intProductID, $intIBlockID, $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, $boolSKU, $boolGetIDS){
      
      self::$pId = $intProductID;
      
      return true;
      
   }
   
   public function OnGetDiscountResult(&$arResult){
      
      $intProductID = self::$pId; 

      if($intProductID){
         $newAr = array();
         foreach($arResult as $key=>$val){
            if(strpos($val["NAME"],"#KMP#")===false || isset($_SESSION['KMP'][$intProductID]) || $_SESSION['KMPBASKET'][$intProductID]) {
               $newAr[] = $val;
            }
         }
         $arResult = $newAr;
      }
      
   }
   
}
 

Вариант 1
class artDiscount extends CCatalogDiscount{
   
   /*const TYPE_PERCENT = 'P';
   const TYPE_FIX = 'F';
   const TYPE_SALE = 'S';

   static protected $arCacheProduct = array();
   static protected $arCacheDiscountFilter = array();
   static protected $arCacheDiscountResult = array();
   static protected $arCacheProductSectionChain = array();
   static protected $arCacheProductSections = array();
   static protected $arCacheProductProperties = array();
   static protected $arCacheProductIds = array();*/
   
   public function GetDiscount($intProductID, $intIBlockID, $arCatalogGroups = array(), $arUserGroups = array(), $strRenewal = "N", $siteID = false, $arDiscountCoupons = false, $boolSKU = true, $boolGetIDS = false)
   {
      global $DB;
      global $APPLICATION;
      global $stackCacheManager;

      $boolSKU = (true === $boolSKU ? true : false);
      $boolGetIDS = (true === $boolGetIDS ? true : false);

      $intProductID = intval($intProductID);
      if (0 >= $intProductID)
      {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_PRODUCT_ID_ABSENT"), "NO_PRODUCT_ID");
         return false;
      }

      $intIBlockID = intval($intIBlockID);
      if (0 >= $intIBlockID)
      {
         $APPLICATION->ThrowException(GetMessage("BT_MOD_CATALOG_DISC_ERR_IBLOCK_ID_ABSENT"), "NO_IBLOCK_ID");
         return false;
      }

      if (!empty($arCatalogGroups))
      {
         if (!is_array($arCatalogGroups))
         {
            $arCatalogGroups = (intval($arCatalogGroups)."|" == $arCatalogGroups."|" ? array($arCatalogGroups) : array());
         }
      }
      else
      {
         $arCatalogGroups = array();
      }
      if (!empty($arCatalogGroups))
      {
         CatalogClearArray($arCatalogGroups);
      }

      if (!is_array($arUserGroups))
      {
         $arUserGroups = (intval($arUserGroups)."|" == $arUserGroups."|" ? array($arUserGroups) : array());
      }
      $arUserGroups[] = 2;
      if (!empty($arUserGroups))
      {
         CatalogClearArray($arUserGroups);
      }

      $strRenewal = (($strRenewal == "Y") ? "Y" : "N");

      if ($siteID === false)
         $siteID = SITE_ID;

      if ($arDiscountCoupons === false)
         $arDiscountCoupons = CCatalogDiscountCoupon::GetCoupons();

      $arSKU = false;

      $arResult = array();
      $arResultID = array();

      $strCacheKey = md5('C'.implode('_', $arCatalogGroups).'-'.'U'.implode('_', $arUserGroups));
      if (!isset(self::$arCacheDiscountFilter[$strCacheKey]))
      {
         $arFilter = array(
            'PRICE_TYPE_ID' => $arCatalogGroups,
            'USER_GROUP_ID' => $arUserGroups,
         );
         $arDiscountIDs = CCatalogDiscount::__GetDiscountID($arFilter);
         if (!empty($arDiscountIDs))
         {
            sort($arDiscountIDs);
         }
         self::$arCacheDiscountFilter[$strCacheKey] = $arDiscountIDs;
      }
      else
      {
         $arDiscountIDs = self::$arCacheDiscountFilter[$strCacheKey];
      }

      $arProduct = array();

      if (!empty($arDiscountIDs))
      {
         $boolGenerate = false;

         $strCacheKey = 'D'.implode('_', $arDiscountIDs).'-'.'S'.$siteID.'-R'.$strRenewal;
         if (!empty($arDiscountCoupons))
         {
            $strCacheKey .= '-C'.implode('|', $arDiscountCoupons);
         }
         $strCacheKey = md5($strCacheKey);

         if (!isset(self::$arCacheDiscountResult[$strCacheKey]))
         {
            $arSelect = array(
               "ID", "TYPE", "SITE_ID", "ACTIVE", "ACTIVE_FROM", "ACTIVE_TO",
               "RENEWAL", "NAME", "SORT", "MAX_DISCOUNT", "VALUE_TYPE", "VALUE", "CURRENCY",
               "PRIORITY", "LAST_DISCOUNT",
               "COUPON", "COUPON_ONE_TIME", "COUPON_ACTIVE", 'UNPACK'            );
            $strDate = date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL")));
            $arFilter = array(
               "ID" => $arDiscountIDs,
               "SITE_ID" => $siteID,
               "TYPE" => DISCOUNT_TYPE_STANDART,
               "ACTIVE" => "Y",
               "RENEWAL" => $strRenewal,
               "+<=ACTIVE_FROM" => $strDate,
               "+>=ACTIVE_TO" => $strDate
            );

            if (is_array($arDiscountCoupons))
            {
               $arFilter["+COUPON"] = $arDiscountCoupons;
            }

            $arDiscountList = array();
            CTimeZone::Disable();
            $rsPriceDiscounts = CCatalogDiscount::GetList(
               array(),
               $arFilter,
               false,
               false,
               $arSelect
            );
            CTimeZone::Enable();
            while ($arPriceDiscount = $rsPriceDiscounts->Fetch())
            {
               $arDiscountList[] = $arPriceDiscount;
            }
            self::$arCacheDiscountResult[$strCacheKey] = $arDiscountList;
         }
         else
         {
            $arDiscountList = self::$arCacheDiscountResult[$strCacheKey];
         }

         if (!empty($arDiscountList))
         {
            foreach ($arDiscountList as &$arPriceDiscount)
            {
               if ($arPriceDiscount['COUPON_ACTIVE'] != 'N')
               {
                  if (!$boolGenerate)
                  {
                     if (!isset(self::$arCacheProduct[$intProductID]))
                     {
                        $arProduct = array('ID' => $intProductID, 'IBLOCK_ID' => $intIBlockID);
                        if (!self::__GenerateFields($arProduct))
                           return false;
                        $boolGenerate = true;
                        self::$arCacheProduct[$intProductID] = $arProduct;
                     }
                     else
                     {
                        $arProduct = self::$arCacheProduct[$intProductID];
                     }
                  }
                  if (CCatalogDiscount::__Unpack($arProduct, $arPriceDiscount['UNPACK']))
                  {
                     unset($arPriceDiscount['UNPACK']);
                     $arResult[] = $arPriceDiscount;
                     $arResultID[] = $arPriceDiscount['ID'];
                  }
               }
            }
            if (isset($arPriceDiscount))
               unset($arPriceDiscount);
         }
      }

      if ($boolSKU)
      {
         $boolSKU = false;
         $arSKU = false;
         $arSKUExt = false;
         $arSKUExt = CCatalogSKU::GetInfoByOfferIBlock($intIBlockID);
         if (!empty($arSKUExt) && is_array($arSKUExt))
         {
            if (isset($arProduct['PROPERTY_'.$arSKUExt['SKU_PROPERTY_ID'].'_VALUE']))
            {
               $arVal = $arProduct['PROPERTY_'.$arSKUExt['SKU_PROPERTY_ID'].'_VALUE'];
               $arSKU = array(
                  'ID' => $arVal[0],
                  'IBLOCK_ID' => $arSKUExt['PRODUCT_IBLOCK_ID'],
               );
               $boolSKU = true;
            }
         }
      }
      if ($boolSKU)
      {
         $arDiscountParent = CCatalogDiscount::GetDiscount($arSKU['ID'], $arSKU['IBLOCK_ID'], $arCatalogGroups, $arUserGroups, $strRenewal, $siteID, $arDiscountCoupons, false, false);
         if (!empty($arDiscountParent))
         {
            if (empty($arResult))
            {
               $arResult = $arDiscountParent;
            }
            else
            {
               foreach ($arDiscountParent as &$arOneParentDiscount)
               {
                  if (in_array($arOneParentDiscount['ID'], $arResultID))
                     continue;
                  $arResult[] = $arOneParentDiscount;
                  $arResultID[] = $arOneParentDiscount['ID'];
               }
               if (isset($arOneParentDiscount))
                  unset($arOneParentDiscount);
            }
         }
      }

      if (!$boolGetIDS)
      {
         $arDiscSave = CCatalogDiscountSave::GetDiscount(array(
            'USER_ID' => 0,
            'USER_GROUPS' => $arUserGroups,
            'SITE_ID' => $siteID
         ));
         if (!empty($arDiscSave))
         {
            $arResult = (!empty($arResult) ? array_merge($arResult, $arDiscSave) : $arDiscSave);
         }
      }
      else
      {
         $arResult = $arResultID;
      }
      
      //тут можно удалить ненужные скидки, или сформировать новые
      $newAr = array();
      foreach($arResult as $key=>$val){
         if(strpos($val["NAME"],"#KMP#")===false || isset($_SESSION['KMP'][$intProductID]) || $_SESSION['KMPBASKET'][$intProductID]) {
            $newAr[] = $val;
         }
      }
      return $newAr;
   }
}


Назад в раздел

Подписаться на новые материалы раздела:
Загрузка...