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

This is an old revision of the document!


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

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

$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_discounts2'), 'shop_item_discounts2.shop_item_id', '=', 'shop_items.id', array(
            array('OR' => array('shop_item_discounts2.shop_item_id', '=', Core_QueryBuilder::expression('`modifications`.`id`')))
        ))
        ->leftJoin(array('shop_discounts', 'shop_discounts2'), 'shop_discounts2.id', '=', 'shop_item_discounts2.shop_discount_id')
 
        ->where('shop_discounts2.active', '=', 1)
        ->where('shop_discounts2.start_datetime', '<', Core_Date::timestamp2sql(time()))
        ->where('shop_discounts2.end_datetime', '>', Core_Date::timestamp2sql(time()))
        ->where('shop_discounts2.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.1489565114.txt.gz · Last modified: 15.03.17 в 11:05 by maximzasorin_gmail.com