gpt4 book ai didi

php - OpenCart - 从产品查询中删除一个类别及其子类别

转载 作者:行者123 更新时间:2023-11-29 23:29:51 24 4
gpt4 key购买 nike

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

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

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

model/catalog/product.phppublic 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'] . "'";
}

ID = 65 类别的产品仍然可见。有人知道该怎么做吗?

最佳答案

我解决了我的问题。

我在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.")";
}

很抱歉解决方案困惑,但时间是这个项目的关键。

关于php - OpenCart - 从产品查询中删除一个类别及其子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com