PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 16, 2022

[FIXED] How to add new variable in Opencart product controller?

 October 16, 2022     opencart, php, variables     No comments   

Issue

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.


Solution

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; ?>


Answered By - K. B.
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing