2014-03-24 104 views
0

如果我想添加一個PHP語句在我的產品頁面模板,看起來像這樣:獲取產品的相關類別的子類別打開車

<?php if (Product Has Parent Category = 146) { 
// Do this 
} elseif (Product Has Parent Category = 130) { 
// Do this 
} else { 
// Do this } ?> 

Ofcourse這個心不是代碼,但我怎麼會做這個?我基本上試圖獲得該子類別的父類別。任何幫助將不勝感激。謝謝!

UPDATE:

每個產品被放置在多個類別。所以我應該有父類別的陣列。這裏是我找到的數據庫結構。

product_to_category

product_id | category_id 

類別

category_id | parent_id | ... 
+0

在數據庫中需要一個表,其中的字段是'id','product','parent-id'。這樣你就可以知道你正在看的產品的父母是什麼。 – Bangash

+0

我剛剛更新了上面的問題,並帶有DB的屏幕截圖。 – JCBiggar

回答

1

catalog/controller/product/product.php找到

$this->load->model('catalog/product'); 
//this will load product model 

添加

$cat_info = $this->model_catalog_product->getCategories($this->request->get['product_id']); 
// this will give all the category of product 

    foreach($cat_info as $cat_id){ 
    $cat = $this->model_catalog_category->getParentCategories($cat_id['category_id']); 
    //this will give the parent category  

     if(!empty($cat)){ 
      foreach($cat as $ids){ 
      $this->data['path_id'][] = $ids['path_id']; 
     } 
     } 
    } 

catalog/model/catalog/category.php添加

public function getParentCategories($category_id) { 
    $query = $this->db->query("SELECT path_id FROM " . DB_PREFIX . "category_path WHERE category_id = '" . (int)$category_id . "' AND '" . (int)$category_id . "'!=path_id"); 

    return $query->rows; 
} 

現在product.tpl

<?php 
if(in_array(20,$path_id)){ 
    echo 'exists'; 
}else{ 
    echo 'not exists'; 
} 
?> 
0

我能弄明白。我寫了這段代碼,並在我的product.tpl上使用它。

<?php 
    $current_product_id = "SELECT `product_id`,`category_id` FROM `oc_product_to_category` WHERE `product_id`='$product_id' "; 
    $current_product_ids = mysql_query($current_product_id); 
    $current_product_cat_ids=''; 

    while($current_product_cat_id = mysql_fetch_array($current_product_ids)){ 
     $current_product_cat_ids.=$current_product_cat_id['category_id'].','; 
    } 

    $parent_cat_path = mysql_query("SELECT `category_id`,`path_id` FROM `oc_category_path` WHERE `category_id` IN (" . rtrim($current_product_cat_ids, ',') . ")"); 
    $parent_cat_id_array=''; 
    while ($parent_cat_paths = mysql_fetch_array($parent_cat_path)) { 
     $parent_cat_id_array.=$parent_cat_paths['path_id'].','; 
    } 

    $parent_cat_id_array_str = implode(',',array_unique(explode(',', $parent_cat_id_array))); 

    if (strpos($parent_cat_id_array_str,'132') !== false) { 
    // Do This Here 
    } else { 
    //Do This Here 
    } ?>