Saahon
Saahon

Reputation: 419

opencart - Get products by price range

The answer to this question was looking at Google from stackoverflow site. But it did not find an answer for my situation.

I need to extract information about product for price range from the data base on OpenCart CMS. To this I added a method to model/catalog/product.php the end of the file:

public function getProductByPrice($low, $heigh){
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE price >= " . $low . " AND price < " . $heigh );

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

There extract information from the only one table. Product name is on the other table. And how do I extract the data directly from the two table and merge the two results into one array?

The OpenCart version: 2.1.0.2 (rs.1)

Upvotes: 0

Views: 523

Answers (1)

linktoahref
linktoahref

Reputation: 7972

To extract data from two tables use JOINS and to get products with data and names restricted by price you could use this query:

$query = $this->db->query("SELECT p.*, pd.name FROM " . DB_PREFIX .
         "product p LEFT JOIN " . DB_PREFIX ."product_description pd
          ON p.product_id = pd.product_id WHERE p.price >= " . (int)$low . 
         " AND p.price < " . (int)$high .");

However,

$this->getProduct($result['product_id']);

would also fetch you the product name based on the product id.

P.S: You have misspelled $high

Upvotes: 1

Related Questions