Приветствую!
Проблема такая:
мне нужно сделать фильтрацию модуля "хиты продаж" по категориям.
Есть вот такая функция:
public function getBestSellerProductsbyCate($category_id,$limit){
if ($this->customer->isLogged()) {
$customer_group_id = $this->customer->getCustomerGroupId();
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
$product_data = $this->cache->get('product.bestseller.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'). '.' . $customer_group_id . '.' . (int)$limit);
if (!$product_data) {
$product_data = array();
$query = $this->db->query("SELECT op.product_id, COUNT(*) AS total FROM " . DB_PREFIX . "order_product op LEFT JOIN `" . DB_PREFIX . "order` o ON (op.order_id = o.order_id) LEFT JOIN `" . DB_PREFIX . "product` p ON (op.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE o.order_status_id > '0' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2c.category_id = '".$category_id."' GROUP BY op.product_id ORDER BY total DESC LIMIT " . (int)$limit);
foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}
$this->cache->set('product.bestseller.' . (int)$this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'). '.' . $customer_group_id . '.' . (int)$limit, $product_data);
}
return $product_data;
}
и переписанный модуль хиты продаж:
.....
$this->load->model('catalog/category');
$this->load->model('tool/image');
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
$products = array();
$results = $this->model_catalog_product->getBestSellerProductsbyCate($category['category_id']);
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
} else {
$image = false;
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
} else {
$special = false;
}
if ($this->config->get('config_review_status')) {
$rating = $result['rating'];
} else {
$rating = false;
}
$products[$category['category_id']][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
);
}
// Level 1
$this->data['categories'][] = array(
'name' => $category['name'],
'products' => $products,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
.....
Вроде работает, но проблема в том что товары для всех категорий определяются из первой категории в массиве - вот пример получаемого массива из getBestSellerProductsbyCate:
Array ( [20] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [18] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [25] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [57] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [17] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [24] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [33] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) ) Array ( [34] => Array ( [0] => Array ( [product_id] => 47 [name] => HP LP3065 ) [1] => Array ( [product_id] => 43 [name] => MacBook ) ) )
Хотя HP LP3065 и MacBook принадлежат только к категории id 20...