public function bestsellingProductsBlock() { ClassLoader::import('application.model.product.ProductFilter'); $cache = $this->application->getCache(); $key = array('bestsellers', $this->getCategory()->getID() . '_' . $days); if (!$cache->get($key)) { $category = $this->getCategory(); $filter = new ProductFilter($category, new ARSelectFilter()); $filter->includeSubcategories(); $filter->setEnabledOnly(); $selectFilter = $filter->getSelectFilter(); $selectFilter->setLimit($this->config->get('BESTSELLING_ITEMS_COUNT')); $selectFilter->setOrder(new ARExpressionHandle('cnt'), 'DESC'); $q = new ARSelectQueryBuilder(); $q->includeTable('Product'); $q->joinTable('Category', 'Product', 'ID', 'categoryID'); $q->addField('Product.ID'); $q->addField(new ARExpressionHandle('(SELECT SUM(count) FROM OrderedItem LEFT JOIN CustomerOrder ON OrderedItem.customerOrderID=CustomerOrder.ID WHERE productID=Product.ID AND CustomerOrder.isPaid=1 AND CustomerOrder.dateCompleted > "' . ARSerializableDateTime::createFromTimeStamp(strtotime('-' . $this->config->get('BESTSELLING_ITEMS_DAYS') . ' days')) . '")'), null, 'cnt'); $q->setFilter($selectFilter); $cache->set($key, ActiveRecord::getDataByQuery($q)); } $products = $cache->get($key); if (!$products) { return; } $ids = array(); foreach ($products as $id) { $ids[] = $id['ID']; } $products = ActiveRecord::getRecordSetArray('Product', select(IN('Product.ID', $ids)), array('DefaultImage' => 'ProductImage')); ProductPrice::loadPricesForRecordSetArray($products); if ($products) { return new BlockResponse('products', $products); } }
public function updateSalesRank() { $cacheFile = ClassLoader::getRealPath('cache.salesrank'); if (!file_exists($cacheFile) || filemtime($cacheFile) < time() - 3600) { touch($cacheFile); ActiveRecord::executeUpdate('UPDATE Product SET salesRank=(SELECT SUM(count) FROM OrderedItem LEFT JOIN CustomerOrder ON OrderedItem.customerOrderID=CustomerOrder.ID WHERE productID=Product.ID AND CustomerOrder.isPaid=1 AND CustomerOrder.dateCompleted > "' . ARSerializableDateTime::createFromTimeStamp(strtotime('-' . $this->config->get('BESTSELLING_ITEMS_DAYS') . ' days')) . '")'); } }
public function getValidatedValue($value) { if (is_numeric($value)) { $value = ARSerializableDateTime::createFromTimeStamp($value); } return $value; }