简体   繁体   English

在子类别打开购物车中获取产品的父类别

[英]Get Parent Category of Product in Sub-Category Open Cart

If I am trying to add a php statement on my product page template that looks like this: 如果我试图在产品页面模板上添加如下所示的php语句:

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

Ofcourse this isnt the code, but how would I do this? 当然这不是代码,但是我该怎么做? Im basically trying to get the parent category that the subcategory is in. Any help would be greatly appreciated. 我基本上是试图获得子类别所在的父类别。任何帮助将不胜感激。 Thanks! 谢谢!

UPDATE: 更新:

Each product is placed in multiple categories.. So I should have an array of Parent categories. 每个产品都放置在多个类别中。因此,我应该具有一系列父类别。 Here is the database structure that I found for this. 这是我为此找到的数据库结构。

product_to_category product_to_category

product_id | category_id

category 类别

category_id | parent_id | ...

In catalog/controller/product/product.php find catalog/controller/product/product.php找到

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

add after 加入之后

$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'];   
         }
       }
    }

In catalog/model/catalog/category.php add 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;
}

now in product.tpl 现在在product.tpl

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

I was able to figure it out. 我能够弄清楚。 I wrote this code and am using it on my product.tpl . 我编写了这段代码,并在我的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 
    } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从WooCommerce中的父类中获取与当前产品关联的相关子类 - Get relevant sub-category associated with the current product from parent category in WooCommerce Prestashop:从子类别页面获取父类别信息 - Prestashop: Get parent category informations from the sub-category page 在magento中获取产品的最后一个子类别 - Getting the last sub-category of a product in magento 在父类别中选择选项时显示子类别列表 - Showing sub-category list when an option select in parent category 根据所选父类别在下拉列表中显示 Wordpress 子类别 - Display Wordpress Sub-category in dropdown based on the selected Parent Category opencart-在子类别页面上获取父类别名称/ ID? 新 - opencart - get parent category name/id on a sub-category page? New 如何使用正确的顺序列出父类别和子类别数据 - How to list parent category and sub-category data with proper order 如何在JSON Laravel中的子类别和子类别用户类别下获取子项 - How to get child under sub-category and sub-category user category in JSON Laravel 在Magento产品页面上显示当前子类别名称 - Displaying Current Sub-Category name on Magento Product page 在子类别页面上显示wordpress子类别帖子 - Display wordpress sub-category posts on sub-category page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM