Back to PhocacartPriceHistory class

Method getPriceHistoryById

public static
getPriceHistoryById
(mixed $productId, mixed $limit = 10, mixed $admin = 0)

Method getPriceHistoryById - Source code

public static function getPriceHistoryById($productId, $limit = 10, $admin = 0)
{
    $date = Factory::getDate();
    $dateNow = $date->toSql();
    $db = Factory::getDBO();
    $query = 'SELECT a.id, a.product_id, a.price, a.date' . ' FROM #__phocacart_product_price_history AS a' . ' WHERE a.product_id = ' . (int) $productId . ' AND a.type IN (0,1)' . ' ORDER BY a.date DESC';
    // set latest e.g. 10 items
    if ((int) $limit > 0) {
        $query .= ' LIMIT ' . (int) $limit;
    }
    $db->setQuery($query);
    $history = $db->loadAssocList();
    if ($admin == 1) {
        return $history;
    }
    // We need to get the outcome from latest to history so we get the e.g. latest 10 items
    // but for displaying we need to start from start
    $history = array_reverse($history);
    $query = 'SELECT a.price' . ' FROM #__phocacart_products AS a' . ' WHERE a.id = ' . (int) $productId . ' ORDER BY a.id';
    $db->setQuery($query);
    $todayDb = $db->loadAssoc();
    $today = array();
    if (isset($todayDb['price']) && $todayDb['price'] > 0) {
        $today[0]['id'] = 0;
        $today[0]['date'] = $dateNow;
        $today[0]['price'] = $todayDb['price'];
    }
    // Join today with price history
    $c = count($history);
    $c--;
    if (isset($history[$c]['date'])) {
        $dateDb2 = HTMLHelper::_('date', $history[$c]['date'], 'Y-m-d');
        $dateNow2 = HTMLHelper::_('date', $dateNow, 'Y-m-d');
        // Date in price history is the same like today's price, so take today's price
        if ($dateDb2 == $dateNow2 && isset($history[$c])) {
            unset($history[$c]);
        }
    }
    if (!empty($history) && !empty($today)) {
        $new = array_merge($history, $today);
    } else {
        if (empty($history) && !empty($today)) {
            $new = $today;
        } else {
            if (!empty($history) && empty($today)) {
                $new = $history;
            } else {
                $new = array();
            }
        }
    }
    // correct the count of items in case the current date was added to the prict history list
    $c2 = count($new);
    if ($c2 > $limit && isset($new[0])) {
        unset($new[0]);
    }
    return $new;
}