opencart - Get products by price range - opencart

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)

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

Related

Select Today's date using BigQuery

I'm using Google Cloud SDK (command-line) via C# and I want to select the information for the Current Date(today).
The select is working but I'm not able to bring the latest date on the column DATE
Below is the query I'm using:
var table = client.GetTable("projectId", "datasetId", "table");
var sql = $"" +
$"SELECT " +
$"sku, " +
$"FROM {table} " +
$"WHERE DATE=CurrentDate('America/Sao_Paulo') " +
$"LIMIT 10";
Schema: SKU - String
DATE - Timestamp
Try to use CURRENT_DATE instead of CurrentDate
var table = client.GetTable("projectId", "datasetId", "table");
var sql = $"" +
$"SELECT " +
$"sku, " +
$"FROM {table} " +
$"WHERE DATE=CURRENT_DATE('America/Sao_Paulo') " +
$"LIMIT 10";

Multiple If conditions with Vlookup and IFNA involved

I am scratching my head and just cannot find the solution for hours. I need the excel to use Cell D(this is the result from another Vlookup) as the result OR Cell A + Cell B as the other result based on the following. (Not really expert with Excel, but do Understand some logic)
if vlookup from column y return " ", then Vlookup another table to get result of CELL D(Show " "if this Vlookup is #N/A)
if Vlookup from column y return #N/A, the Vlookup another table to get result of Cell D (Show " " if this Vlookup is #N/A)
if Vlookup from column Z return " ", the Vlookup another table to get result of Cell D (Show " ",if this Vlookup is #N/A)
if Vlookup from column Z return #N/A, the Vlookup another table to get result of Cell D (Show " "if this Vlookup is #N/A)
if any if the above returns other than " " or #N/A, then just do CELL A + CELL B = Cell D
Here is an attempt but unfortunately it doesn't work:
=if(and(vlookup(A18,DEALS!A:P,16,false)<>""),F18-E18,
if(and(vlookup(A18,DEALS!A:P,14,false)<>""),F18-E18,
if(and(IFNA(vlookup(A18,DEALS!A:P,16,false))=false),F18-E18,
if(and(IFNA(vlookup(A18,DEALS!A:P,14,false))=false),F18-E18,
"another Vlookup" ))))
Note: Edited in as OP does not have edit rights. Line-breaks added to formula for legibility.

opencart - I am trying to list weightunit in featured products and products list in category.tpl

I am trying to list weightunit in featured and products list in category.tpl and running into issues. Anyone know how to achieve selecting and displaying weightunit in featured.tpl and category.tpl. thanks.
First create a method to you category method so that you can get all weight units.
public function getWeightClasses() {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
return $query->rows;
}
Now call this from your category controller
$this->load->model('catalog/category');
$data['weightUnits'] = $this->model_catalog_category->getWeightClasses();
print_r($data['weightUnits']); // all weight units

How to use date() function / return hydrated objects?

In my Entity Repository for Doctrine2, I have the following:
$date = new DateTime('NOW');
$date = $date->format('Y-m-d');
if ($region) {
$region_sql = " AND WHERE region LIKE ?3 ";
} else {
$region_sql = "";
}
$sql = "SELECT *, count(month) as count FROM (SELECT *, date(date_from, 'start of month', '+1 month', '-1 day') as month FROM manifestations WHERE date_to >= :date_to " . $region_sql . ") GROUP BY month";
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindValue(':date_to', $date);
if($region) {
$stmt->bindValue(3, sprintf('%%,%s,%%', $region));
}
$stmt->execute();
return $stmt->fetchAll();
But I need to change this so that it returns the objects hydrated instead of an array. I originally wanted to use DQL or queryBuilder but could not find a way to get the date() function to work.
With NativeQuery you can execute native SELECT SQL statements and map the results to Doctrine entities or any other result format supported by Doctrine.
What you want to do can be achieved using the ResultSetMappingBuilder.
ResultSetMappingBuilder is a convenience wrapper. It can generate the mappings for you based on Entities.
This is how I'd do it (I assume your query works, maybe you'll have to adjust it, as I will use a new alias):
Create the ResultSetMapping:
use Doctrine\ORM\Query\ResultSetMapping;// Don't forget this
$rsm = new ResultSetMappingBuilder($entityManager);// $entityManager points to your entity manager.
$rsm->addRootEntityFromClassMetadata('path/to/class/MyClass', 'a');// Notice the a, it's an alias that I'll later on use in the query.
$rsm->addScalarResult("count", "count");// column, alias
Prepare $region_sql part as you do in your code and add the a alias to whatever you want to map. a.* will be mapped to an object (notice the as a I use in the query):
$sql = "SELECT a.*, count(month) as count FROM (SELECT *, date(date_from, 'start of month', '+1 month', '-1 day') as month FROM manifestations WHERE date_to >= :date_to " . $region_sql . ") as a GROUP BY month";
Execute the query:
$query = $entityManager->createNativeQuery($sql, $rsm);
$query->setParameter('date_to', $date);
$result = $query->getResult();
This will give you an array of rows. Each of them will be a mixed array, $result[n][0] will contain the object and $result[n]["count"] the value of the count column of the query (name of the column is the same as the alias we set up in the $rsm) where n is the number of the row.

update a column from one table with multiplied value from column from another table

I have 2 tables, first product with a column price and second product_reward with a column points.
I'd like to update the column points with the value from column price*0.1.
So points=price*01.
Thanks!
You can use following sql query to update current reward points of products.
UPDATE `oc_product_reward` rp SET rp.`points`=(SELECT p.price FROM `oc_product` p WHERE rp.`product_id` = p.`product_id`)*0.1
After that, you have to update your product model of admin part. insert and update part of upload/admin/model/catalog/product.php
if (isset($data['product_reward'])) {
foreach ($data['product_reward'] as $customer_group_id => $product_reward) {
$this->db->query("INSERT INTO " . DB_PREFIX . "product_reward SET product_id = '" . (int)$product_id . "', customer_group_id = '" . (int)$customer_group_id . "', points = '" . (int)((float)$data['price'] * 0.1) . "'");
}
}