0
我需要在OpenCart 2的主頁上顯示某個類別的項目。我該怎麼做? 從控制器我的代碼:如何在OpenCart 2中顯示某個類別的項目?
$products_info = $this->model_catalog_product->getProducts();
$data['products'] = $products_info;
我需要在OpenCart 2的主頁上顯示某個類別的項目。我該怎麼做? 從控制器我的代碼:如何在OpenCart 2中顯示某個類別的項目?
$products_info = $this->model_catalog_product->getProducts();
$data['products'] = $products_info;
假設你第一次加載你調用模型:
$this->load->model('catalog/product');
所有你需要做的就是定義設置的陣列傳遞給getProducts()
。在這種情況下,你可以逃脫只發送你想獲得產品的類別ID:
$filter_data = array(
'filter_category_id' => $category_id,
);
然後調用函數像你一樣:
$results = $this->model_catalog_product->getProducts($filter_data);
然後遍歷產品,就好像你是在一個類別viewto數據傳遞給視圖:
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
} else {
$image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
}
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_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
);
}
現在你有一系列的產品來顯示,但是你會在你的TPL喜歡。
爲什麼你得到$ this-> request-> get ['product_id'],這應該是$ this-> request-> get ['category_id'],因爲你正在獲得某個類別的產品。 –
在您的控制器中添加此項,$ products_info = $ this-> model_catalog_product-> getCatProducts(您的類別ID);並在您的模型中創建getCatProducts($ catid)函數並編寫自己的查詢並將類別標識傳遞給它,這將適用於您。 –