1

当客户插入最小值和最大值时,我尝试过滤产品价格。

产品.php

public function getProducts($data = array()) {
....

if (!empty($data['min'])) {
    $sql .= " AND p.price BETWEEN '" .$data['min']. "' AND 9999 ";
}

使用此代码,类别页面将列出从价格 x 到 9999 的所有产品。

问题是某些产品具有tax并且值存储在另一个表中(oc_tax_rate)。那么如何在查询中获得产品的最终价格呢? (p.price + tax),像这样:

if (!empty($data['min'])) {
    $sql .= " AND final_price BETWEEN '" .$data['min']. "' AND 9999 ";
}

当我检查页面控制器/category.php

$this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))

上面的代码将返回最终价格。

超频:v1.5.6

4

1 回答 1

1

First of all, when You use a min and max filter values, Your query should not be choosing between min and 9999 and between 0 and max but You should do this:

if (!empty($data['min'])) {
    $sql .= " AND p.price >= " . (int)$data['min'];
//                               ^^^^^ - against SQL injection!!!
}

and

if (!empty($data['max'])) {
    $sql .= " AND p.price <= " . (int)$data['max'];
//                               ^^^^^ - against SQL injection!!!
}

Also, as mentioned in the comment, do not forget to cure Your code against SQL injection!

Now, to Your real question: It is not possible to get the taxes and calculate taxed price along with products getting in one query. But You could do this in Your model - find this part of code

foreach ($query->rows as $result) {
    $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}

and we will modify it this way:

foreach ($query->rows as $result) {
    if (!empty($data['min']) 
            && $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')) >= $data['min']) {
        $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
    } else if (!empty($data['max']) 
            && $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')) <= $data['max']) {
        $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
    } else {
        $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
    }
}
于 2013-11-04T09:16:46.563 回答