OpenCart - 从产品查询中删除一个带有子类别的类别

时间:2014-10-30 11:45:19

标签: php mysql opencart

我一直在努力改变getProducts($data = array())方法中的查询。

所以..在特定控制器中,我在exclude_category_id => 65数组中添加了$data,我将其传递给getProducts($data)

$data = array(
    'sort'  => 'p.date_added',
    'order' => 'DESC',
    'start' => 0,
    'limit' => $setting['limit'],
    'exclude_category_id' => 65
);

model/catalog/product.php的{​​{1}}中,我添加了以下代码:

public function getProducts($data = array())

在这种方法中我也加入了:

if (!empty($data['filter_category_id'])) {
    if (!empty($data['filter_sub_category'])) {
        $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";         
    } else {
        $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
    }

    if (!empty($data['filter_filter'])) {
        $sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
    } else {
        $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
    }
} else if(!empty($data['exclude_category_id'])){

    // THIS ELSE IF IS ADDED BY ME

    $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
    $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
}else {
    $sql .= " FROM " . DB_PREFIX . "product p";
}

但是if(!empty($data['exclude_category_id'])){ $sql .= " AND p2c.category_id != '" . (int)$data['exclude_category_id'] . "'"; } 类别的产品仍然可见。任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。

我在model/catalog/product.php的方法开头添加了

public function getProducts($data = array()) {
    $exclude_ids;

    if(!empty($data['exclude_category_id'])){
        $exclude_ids = array($data['exclude_category_id']);

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

        $results = $this->model_catalog_category->getCategories($data['exclude_category_id']);

        if($results){
            foreach($results as $result){
                $exclude_ids[] = $result['category_id'];
            }
        }
    }

    if(!empty($exclude_ids)){
        $t = $exclude_ids;
        $exclude_ids = "";

        foreach($t as $id){
            $exclude_ids .= $id.', ';
        }

        $exclude_ids = substr($exclude_ids, 0, -2);
    }

以及此功能的更深层次:

    if (!empty($data['filter_category_id'])) {
        if (!empty($data['filter_sub_category'])) {
            $sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";         
        } else {
            $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
        }

        if (!empty($data['filter_filter'])) {
            $sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
        } else {
            $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
        }
    } else if(!empty($data['exclude_category_id'])){ /*********** ADDED ELSE IF *********/
        $sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
        $sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
    }else {
        $sql .= " FROM " . DB_PREFIX . "product p";
    }

更深一点:

    if(!empty($data['exclude_category_id'])){
        $sql .= " AND p2c.category_id NOT IN (".$exclude_ids.")";
    }

对不起混乱的解决方案,但TIME是这个项目的关键。