2013-05-09 99 views
1

是否可以在每個類別中顯示特殊產品,僅顯示與該類別相關的產品?例如,如果類別是「諾基亞」,則顯示僅限諾基亞使用的特價商品,並且不顯示其他特價商品。顯示每個類別的特殊產品,僅顯示與該類別相關的產品

也適用於特色產品。

可能嗎?如果可能的話,你能解釋一下如何做到讓初學者能理解它嗎?我正在使用OpenCart 1.5.3.1。

+0

當然有可能。雖然我不確定這是一個初學者,但任務。在'catalog/controller/module/special.php'中,你需要檢查當前頁面是否是一個類別頁面,然後得到它的類別id,然後運行一個循環來檢查和過濾相關產品的產品。 – 2013-05-09 08:11:07

回答

1

好吧,這裏有一些東西讓你開始。我正在使用Opencart 1.5.5.1。 在catalog/controller/module/special.php找到這一行接近尾聲:

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/special.tpl')) { 

之前,添加以下代碼(我希望我的意見使它足夠清晰):

// filter specials by current category code - - - - - - - 

/* check wether current page is a category page (i.e. has 'path' var) */ 
if (isset($this->request->get['path'])) { 

    /* get category ID from path (last number) */ 
    $parts = explode('_', (string)$this->request->get['path']); 
    $category_id = (int)array_pop($parts); 

    /* loop through products */ 
    foreach ($this->data['products'] as $k => $item){ 
     /* check whether this product is assigned to current category */ 
     $sql = "SELECT * FROM " . DB_PREFIX ."product_to_category WHERE product_id = ". $item['product_id']. " AND category_id = ".$category_id; 

     $query = $this->db->query($sql); 

     /* if no match found, remove this item */ 
     if (count($query->rows) == 0){ 
      unset ($this->data['products'][$k]); 
     } 

    } 
} 

// end of filter specials by current category code - - - - 
+0

Thankyou B-and-P,你給出了一個很棒的代碼yar thankyou謝謝你這麼多 – user2365268 2013-05-09 10:31:12

+0

Thankyou B-and-P,它的工作很棒,並且可以在特色產品中將上面的內容分開。請告訴我,我是opencart的初學者。感謝您的幫助 – user2365268 2013-05-09 10:33:55

+0

如果我解決了您的問題,請點擊左邊的複選標記接受我的答案。不要爲你做所有的工作,我建議你自己嘗試並使用我的示例到特色模塊。這是學習的唯一途徑。如果您遇到麻煩,請回到這裏併發布您的代碼。 – 2013-05-09 10:51:41

0

只是這需要我,這裏是我的解決方案,需要修改catalog/model/catalog/product.phpcatalog/controller/module/special.php。我建議VQMod做到這一點:

product.php取代

$sql = "SELECT DISTINCT ps.product_id, (SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = ps.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating FROM " . DB_PREFIX . "product_special ps LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) GROUP BY ps.product_id"; 

有:

$sql = "SELECT DISTINCT ps.product_id, (SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = ps.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating".(isset($data['category_id']) ? ", (SELECT category_id FROM oc_product_to_category WHERE product_id = ps.product_id AND category_id = '".$data['category_id']."' GROUP BY category_id) as category" : "")." FROM " . DB_PREFIX . "product_special ps LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) GROUP BY ps.product_id".(isset($data['category_id']) ? ",category HAVING category='".$data['category_id']."'" : ""); 

special.php:添加

if (isset($this->request->get['path'])) { 
    $parts = explode('_', (string)$this->request->get['path']); 
    $data['category_id'] = (int)array_pop($parts); 
} 

$results = $this->model_catalog_product->getProductSpecials($data); 
相關問題