private:koding:hostcms:modules:shop:useful:items_with_discounts

Вывод товаров со скидками

Иногда на сайтах существует раздел со скидками, в котором выводятся все товары, которые можно купить со скидкой, чтобы оргинизовать такой раздел можно использовать следующее условие для выборки товаров:

$Shop_Controller_Show->shopItems()
    ->queryBuilder()
        ->leftJoin(array('shop_items', 'modifications'), 'modifications.modification_id', '=', 'shop_items.id', array(
            array('AND' => array('modifications.deleted', '=', 0)),
            array('AND' => array('modifications.active', '=', 1)),
        ))
        ->leftJoin(array('shop_item_discounts', 'shop_item_discounts_filter'), 'shop_item_discounts_filter.shop_item_id', '=', 'shop_items.id', array(
            array('OR' => array('shop_item_discounts_filter.shop_item_id', '=', Core_QueryBuilder::expression('`modifications`.`id`')))
        ))
        ->leftJoin(array('shop_discounts', 'shop_discounts_filter'), 'shop_discounts_filter.id', '=', 'shop_item_discounts_filter.shop_discount_id')
 
        ->where('shop_discounts_filter.active', '=', 1)
        ->where('shop_discounts_filter.start_datetime', '<', Core_Date::timestamp2sql(time()))
        ->where('shop_discounts_filter.end_datetime', '>', Core_Date::timestamp2sql(time()))
        ->where('shop_discounts_filter.deleted', '=', 0)
 
        ->where('shop_items.modification_id', '=', 0);

Условие выбирает товары, для которых есть активная скидка, или у которых есть хотя бы одна модификация с активной скидкой.

Условие может работать медленно на больших базах товаров из-за оператора OR для выборки модификаций, можно обойтись без него, если выполнять проверку на наличие скидок у модификаций необязательно.

Обычно условие накладывается в ТДС, например, так:

if (Core_Array::get('discounts'))
{
	// Условие выборки товаров
}

Страницу создал Максим Засорин 15.03.17 в 10:47

private/koding/hostcms/modules/shop/useful/items_with_discounts.txt · Last modified: 27.06.17 в 16:03 by maximzasorin_gmail.com