Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Bitrix\Catalog\DiscountTable;
- use Bitrix\Iblock\ElementTable;
- use Bitrix\Main\Data\Cache;
- use Bitrix\Main\Loader;
- /**
- * Class Discount
- *
- * Служит для создания списка идентификаторов товаров со скидкой
- * для последующей филтрации где-либо
- *
- * @author pinguinjkeke
- * @see http://dev.1c-bitrix.ru/community/webdev/user/265201/blog/12545/
- */
- class DiscountFilter
- {
- /**
- * Время кэширования
- *
- * @var int
- */
- const CACHE_TIME = 3600000;
- /**
- * Идентификатор инфоблока товаров
- *
- * @var int
- */
- const PRODUCT_IBLOCK_ID = 32;
- /**
- * Поля товаров (если необходима фильтрация по другим полям, то их следует добавить сюда,
- * а ненужные исключить)
- *
- * @var array
- */
- private $criteriaFields = [
- 'ID', 'IBLOCK_SECTION_ID', 'IBLOCK_ID', 'NAME'
- ];
- /**
- * Свойства товаров (добавить необходимые, лишние исключить)
- *
- * @var array
- */
- private $criteriaProperties = [
- 'ALCO', 'REGION', 'COUNTRY', 'newproduct', 'popular'
- ];
- /**
- * Дополнительная фильтрация
- *
- * @var array
- */
- private $additionalFilter = [
- 'ACTIVE' => 'Y'
- ];
- /**
- * ID товаров со скидками
- *
- * @var array
- */
- private $ids;
- /**
- * Конструктор
- */
- public function __construct()
- {
- $cache = Cache::createInstance();
- if ($cache->startDataCache(self::CACHE_TIME, 'MESHGROUP_DISCOUNT_FILTER')) {
- Loader::includeModule('catalog');
- Loader::includeModule('iblock');
- Loader::includeModule('sale');
- foreach ($this->criteriaProperties as $property) {
- $this->criteriaFields[] = "PROPERTY_{$property}";
- }
- $ids = $this->processDiscountConditions($this->getDiscounts(), $this->getProducts());
- if (empty($ids)) {
- $cache->abortDataCache();
- return;
- }
- $cache->endDataCache(compact('ids'));
- } else {
- extract($cache->getVars());
- }
- $this->ids = $ids;
- }
- /**
- * Возвращает список идентификаторв товаров со скидкой
- *
- * @return array
- */
- public function getIds()
- {
- return $this->ids;
- }
- /**
- * Получение списка товаров по 1000
- *
- * @return array
- */
- private function getProducts()
- {
- $products = [];
- $filter = array_merge(['IBLOCK_ID' => self::PRODUCT_IBLOCK_ID], $this->additionalFilter);
- $elementsCount = ElementTable::getCount($filter);
- $page = 1;
- while (($page * 1000) < $elementsCount) {
- $get = CIBlockElement::GetList(
- ['ID' => 'asc'],
- $filter,
- false,
- ['nPageSize' => 1000, 'iNumPage' => $page],
- $this->criteriaFields
- );
- while ($res = $get->Fetch()) {
- if (!isset($products[$res['ID']])) {
- if (isset($res['IBLOCK_SECTION_ID'])) {
- $res['SECTION_ID'] = [$res['IBLOCK_SECTION_ID']];
- unset($res['IBLOCK_SECTION_ID']);
- }
- $products[$res['ID']] = $res;
- }
- }
- $page++;
- }
- return $products;
- }
- /**
- * Получение списка скидок
- *
- * @return array
- */
- private function getDiscounts()
- {
- return DiscountTable::getList([
- 'select' => ['ID', 'CONDITIONS_LIST'],
- 'filter' => ['ACTIVE' => 'Y']
- ])->fetchAll();
- }
- /**
- * Обработка условий скидок
- *
- * @param array $discounts Массив условий скидок
- * @param array $products Массив товаров
- * @return array|bool
- */
- private function processDiscountConditions(array $discounts, array $products)
- {
- $condition = new CCatalogCondTree;
- if (!$bool = $condition->Init(BT_COND_MODE_GENERATE, BT_COND_BUILD_CATALOG, [])) {
- return false;
- }
- $ids = [];
- foreach ($discounts as $discount) {
- $filter = $condition->Generate($discount['CONDITIONS_LIST'], ['FIELD' => '$product']);
- foreach ($products as $product) {
- if (eval("return {$filter};")) {
- $ids[] = $product['ID'];
- }
- }
- }
- return $ids;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement