0

I stuck with an Opencart issue, it is old version 1.5.6.4. I want to show the lowest special price on the product page.

Firstly I created a function in the model with the SQL query:

public function getSpecialPriceMin($product_id) {
$query = $this->db->query("SELECT MIN(price) FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
return $query->row;
}

Then I tried to save the result in a variable in the product controller (without success as I am receiving an error message undefined variable):

$data['myVariable'] = $this->model_catalog_product->getSpecialPriceMin($product_id);

And then I try to echo the variable on the product page:

<?php echo $myVariable; ?>

I would appreciate any help. Thank you in advance.

Siddharth Rathod
  • 634
  • 1
  • 7
  • 21
ZoltanB
  • 3
  • 2

1 Answers1

0

If you use OC 1.5.x.x version you should use: in model file:

public function getSpecialPriceMin($product_id) {
$query = $this->db->query("SELECT MIN(price) AS min_price FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
return $query->row;
}

Controller:

$query = $this->model_catalog_product->getSpecialPriceMin($product_id);
$this->data['myVariable'] = $query['min_price'];

And then you can to echo the variable on product page:

<?php echo $myVariable; ?>
K. B.
  • 1,388
  • 2
  • 13
  • 25
  • Hello and thank you for your answer. I tried the $this keyword and in this case I am getting another message Notice: Array to string conversion. – ZoltanB Jun 06 '22 at 08:54
  • My sql query should result only a single value (the minimum special price), is it necessary to store this value in an array? – ZoltanB Jun 06 '22 at 08:56
  • I have edited my answer. please try again. – K. B. Jun 06 '22 at 09:33
  • Super, thank you it works. I am a beginner in sql, could you please help me understand the min_price you used is an alias? – ZoltanB Jun 06 '22 at 11:12
  • One more thing I want to accomplish. I want to query the minimum price 1 month minus current date. I tried to extend my query with this: `AND date_end > DATE_ADD(NOW(), INTERVAL -1 MONTH)` but I get this error message: Parse error: syntax error, unexpected 'MONTH' (T_STRING), expecting ',' or ')' could you help me how to use the MONTH function properly in this case? Thank you – ZoltanB Jun 06 '22 at 11:17
  • Regarding alias `min_price` you can use what you want. Regarding the next question you should to check sql syntax in your query. An error prompts you what is wrong...You can read this: https://stackoverflow.com/questions/1424999/get-the-records-of-last-month-in-sql-server And if my answer was useful you can mark it as useful. – K. B. Jun 06 '22 at 12:51