Back to PhocacartDiscountProduct class

Method getProductsDiscounts

public static
getProductsDiscounts
(mixed $params = array())

Method getProductsDiscounts - Source code

/* Module - get all products with discounts */
public static function getProductsDiscounts($params = array())
{
    $db = Factory::getDBO();
    $user = PhocacartUser::getUser();
    $userLevels = implode(',', $user->getAuthorisedViewLevels());
    $userGroups = implode(',', PhocacartGroup::getGroupsById($user->id, 1, 1));
    $wheres = array();
    //$wheres[]   = "a.product_id = " . (int)$id;
    $wheres[] = " a.access IN (" . $userLevels . ")";
    // ACCESS Discount
    $wheres[] = " p.access IN (" . $userLevels . ")";
    // ACCESS Product
    $wheres[] = " c.access IN (" . $userLevels . ")";
    // ACCESS Category
    $wheres[] = "p.published = 1";
    // PUBLISHED Product
    $wheres[] = "c.published = 1";
    // PUBLISHED Category
    $wheres[] = " (gp.group_id IN (" . $userGroups . ") OR gp.group_id IS NULL)";
    $wheres[] = " (gc.group_id IN (" . $userGroups . ") OR gc.group_id IS NULL)";
    $wheres[] = " (ga.group_id IN (" . $userGroups . ") OR ga.group_id IS NULL)";
    // GROUP
    // STOCK
    if (isset($params['stock_check']) && $params['stock_check'] == 1) {
        $wheres[] = " p.stock > 0";
    }
    // LIMIT
    $limit = 1;
    if (isset($params['limit'])) {
        if ((int) $params['limit'] == 0) {
            return false;
        } else {
            if ((int) $params['limit'] > 0) {
                $limit = (int) $params['limit'];
            }
        }
    }
    $currentDate = PhocacartDate::getCurrentDate();
    $wheres[] = " ((a.valid_from <= '" . $currentDate . "' AND a.valid_to >= '" . $currentDate . "'))";
    $where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
    $query = 'SELECT a.id AS discount_id, a.product_id AS discount_product_id, a.title AS discount_title, a.alias AS discount_alias,' . ' a.discount AS discount_discount, a.access AS discount_access, a.calculation_type AS discount_calculation_type,' . ' a.quantity_from AS discount_quantity_from, a.valid_from AS discount_valid_from, a.valid_to AS discount_valid_to,' . ' a.background_image, a.description AS discount_description,' . ' p.id, p.title, p.alias, p.description, p.image, p.price, p.unit_amount,' . ' p.unit_unit, p.stock, p.stockstatus_a_id, p.stockstatus_n_id,' . ' c.id AS category_id, c.title AS category_title, c.alias AS category_alias,' . ' t.id as tax_id, t.tax_rate AS tax_rate, t.calculation_type AS tax_calculationtype, t.title AS tax_title,' . ' MIN(ppg.price) as group_price' . ' FROM #__phocacart_product_discounts AS a' . ' LEFT JOIN #__phocacart_products AS p ON a.product_id = p.id' . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.product_id = p.id' . ' LEFT JOIN #__phocacart_categories AS c ON c.id = pc.category_id' . ' LEFT JOIN #__phocacart_taxes AS t ON t.id = p.tax_id' . ' LEFT JOIN #__phocacart_item_groups AS ga ON a.id = ga.item_id AND ga.type = 4' . ' LEFT JOIN #__phocacart_item_groups AS gp ON p.id = gp.item_id AND gp.type = 3' . ' LEFT JOIN #__phocacart_item_groups AS gc ON c.id = gc.item_id AND gc.type = 2' . ' LEFT JOIN #__phocacart_product_price_groups AS ppg ON p.id = ppg.product_id AND ppg.group_id IN (SELECT group_id FROM #__phocacart_item_groups WHERE item_id = p.id AND group_id IN (' . $userGroups . ') AND type = 3)' . $where . ' GROUP BY a.id' . ' ORDER BY a.id' . ' LIMIT ' . (int) $limit;
    $db->setQuery($query);
    $items = $db->loadObjectList();
    if (!empty($items) && isset($items[0]->discount_id) && (int) $items[0]->discount_id > 0) {
        return $items;
    }
    return false;
}