Back to PhocacartProduct class

Method getBestSellingProducts

public static
getBestSellingProducts
(mixed $limit = 5, mixed $dateFrom = '', mixed $dateTo = '', mixed $count = false)

Method getBestSellingProducts - Source code

public static function getBestSellingProducts($limit = 5, $dateFrom = '', $dateTo = '', $count = false)
{
    $db = Factory::getDBO();
    $wheres = array();
    $wheres[] = " o.id > 0";
    if ($dateTo != '' && $dateFrom != '') {
        $dateFrom = $db->Quote($dateFrom);
        $dateTo = $db->Quote($dateTo);
        $wheres[] = ' DATE(od.date) >= ' . $dateFrom . ' AND DATE(od.date) <= ' . $dateTo;
    }
    $where = count($wheres) ? ' WHERE ' . implode(' AND ', $wheres) : '';
    if ($count) {
        $q = ' SELECT COUNT(o.id)' . ' FROM #__phocacart_order_products AS o' . ' LEFT JOIN #__phocacart_products AS a ON a.id = o.product_id';
        if ($dateTo != '' && $dateFrom != '') {
            $q .= ' LEFT JOIN #__phocacart_orders AS od ON od.id = o.order_id';
        }
        $q .= $where;
        if ((int) $limit > 0) {
            $q .= ' LIMIT ' . (int) $limit;
        }
        $db->setQuery($q);
        $products = $db->loadResult();
    } else {
        $q = ' SELECT o.product_id AS id, o.title, o.alias, COUNT( o.id ) AS count_products' . ' FROM #__phocacart_order_products AS o';
        //. ' LEFT JOIN #__phocacart_products AS a ON a.id = o.product_id';
        if ($dateTo != '' && $dateFrom != '') {
            $q .= ' LEFT JOIN #__phocacart_orders AS od ON od.id = o.order_id';
        }
        $q .= $where . ' GROUP BY o.product_id, o.title, o.alias' . ' ORDER BY count_products DESC';
        if ((int) $limit > 0) {
            $q .= ' LIMIT ' . (int) $limit;
        }
        $db->setQuery($q);
        $products = $db->loadObjectList();
    }
    /* For now we don't need SEF url, if SEF url is needed, we need to get category alias and category id
     * This cannot be done in sql as then because of table jos_phocacart_product_categories will count count duplicities
     */
    /*
            $productsA = array();
            if (!empty($products)) {
                foreach ($products as $k => $v) {
                    if (isset($v->id)) {
                        $productsA[] = (int)$v->id;
                    }
                }
            }
            $productsS = '';
            if (!empty($productsA)) {
                $productsS = implode(',', $productsA);
            }
    
            $categories = array();
            if ($productsS != '') {
                $query = 'SELECT pc.product_id AS id, c.id AS catid, c.title AS cattitle, c.alias AS catalias'
                . ' FROM #__phocacart_categories AS c'
                . ' LEFT JOIN #__phocacart_product_categories AS pc ON pc.category_id = c.id'
                . ' LEFT JOIN #__phocacart_products AS p ON p.id = pc.product_id'
                . ' WHERE pc.product_id IN ('.$productsS.')'
                . ' GROUP BY pc.product_id';
                $db->setQuery( $query );
    
                $categories = $db->loadObjectList();
    
            }
            if (!empty($categories) && !empty($products)) {
                foreach($products as $k => &$v) {
                    foreach($categories as $k2 => $v2) {
                        if (isset($v->id) && isset($v2->id) && (int)$v->id > 0 && (int)$v->id == (int)$v2->id) {
                            $v->catid 		= $v2->catid;
                            $v->catalias 	= $v2->catalias;
                            $v->cattitle	= $v2->cattitle;
                        }
                    }
                }
    
            }*/
    return $products;
}