OpenCart, display the total number of products - php

I'm using OpenCart ecommerce, and I would like to see in the index of ecommerce the total number of products
I do not have (for now) access to the db opencart, so to understand what is the structure of the db I have referred to this image
And this is an example of a query that I'm trying to use to display the total number of products (the result is obviously not what I expect)
//Test Count Product
//$query_test = $db->query("SELECT " . DB_PREFIX . "product_description.name FROM " . DB_PREFIX . "product INNER JOIN " . DB_PREFIX . "product_description ON " . DB_PREFIX . "product.product_id = " . DB_PREFIX . "product_description.product_id");
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test as $row) {
$count_test++;
}
echo $count_test;

Try this:
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product");
echo $query->row['total'];

Nicolo,
Your code is also correct. For that you need to use this code
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test->rows as $row) {
$count_test++;
}
echo $count_test;
But, I will recommend Monkeyman's way to get product_totals but with some modification (Monkeyman's code will NOT work for multiple stores)
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product_to_store WHERE store_id = '" . (int)$this->config->get('config_store_id') . "'");
echo $query->row['total'];

Related

combine left join into my queries but failed

Tried to combine another query to my existing queries but failed.
Here is the working query without LEFT JOIN
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'");
Below is the one tried to combined (not working)
$order_query = $this->db->query("SELECT * FROM " . DB_PREFIX .
"order_items WHERE order_id = '" . (int)$order_id . "'" LEFT JOIN .
DB_PREFIX ."item_description WHERE item_id= '" . (int)$item_id . "'"
);
Any idea where did i do wrong ?
You need to construct the join statement correctly. The WHERE clauses come in later.
$prefix = DB_PREFIX;
$sql = "
SELECT oi.* FROM {$prefix}order_items AS oi
LEFT JOIN {$prefix}item_description AS itd
ON oi.item_id = itd.item_id
WHERE oi.order_id = ?
AND itd.item_id = ?
";
$this->db->query($sql, [$order_id, $item_id]);
You can try this code
$order_query = $this->db->query("SELECT oi.* FROM " . DB_PREFIX ."order_items oi
LEFT JOIN ".DB_PREFIX ."item_description des on oi.order_id = des.order_id
WHERE oi.order_id = '" . (int)$order_id . "' and
des.item_id= '" . (int)$item_id . "'");

Read value of product's custom options in custom script of Opencart 2.x

Is is possible to read variable value of custom option using key value pair?
I am trying to read option value for a product in custom function.I am able to read value by referring it with array index like below
$option = array_filter($this->request->post['option']);
$product_serial_no = $option['93'] ;
I get magic array index $option[93] by reading variable value in Neatbean debug mode.
But option index is changing for different products thus I want to read value of variable 'Serial Number' using something like associative array.
Is it possible?
The options on product page look like below image.
when you are viewing the product page and say you select some of the options and click "add to cart" the post will carry this array
$_POST['option'] = array(93 => '22222') (as you have presented in your example)
the 93 is the $product_option_id and it allows you to pull all the information you need with this request:
$product_id = $this->request->post['product_id'];
foreach ($this->request->post['option']) as $product_option_id => $value) {
$option_query = $this->db->query("SELECT
po.product_option_id,
po.option_id,
od.name,
o.type
FROM " . DB_PREFIX . "product_option po
LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id)
LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id)
WHERE po.product_option_id = '" . (int)$product_option_id . "'
AND po.product_id = '" . (int)$product_id . "'
AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_query->row);
}
This info should be sufficient enough to do any manipulations with options.
if the option has type of select or radio you may want to query also the possible variations like this
//... put this inside the foreach loop, listed above right after print_r($option_query->row);
if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio') {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
ovd.name,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id)
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$value . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
And in case you have a checkbox (like in the example above)
//...also place this in the loop foreach in the code above.
if ($option_query->row['type'] == 'checkbox' && is_array($value)){
foreach ($value as $product_option_value_id) {
$option_value_query = $this->db->query("SELECT
pov.option_value_id,
pov.quantity,
pov.subtract,
pov.price,
pov.price_prefix,
pov.points,
pov.points_prefix,
pov.weight,
pov.weight_prefix,
ovd.name
FROM " . DB_PREFIX . "product_option_value pov
LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (pov.option_value_id = ovd.option_value_id)
WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "'
AND pov.product_option_id = '" . (int)$product_option_id . "'
AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
print_r($option_value_query->row);
}
}
You can view this exact code in system/library/cart/cart.php
hope this helps.

Mysql add value to the value opencart

well, im trying to get the shipping cost for each product but i have no idea to get the shipping cost correctly, example on my product_shipping_cost table
product_id, shipping_cost
1, 12
2, 15
etc.....
now i try to get single product shipping_cost using this query
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
$cost = $query->row['shipping_cost'];
yes, i got the shipping cost perfectly! but what if i want to get two or more shipping cost? i mean sum the shipping_cost with the selected product_id so it would be 12+15=27 shipping cost, i use this with no luck..
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
foreach($query->row as $shipping) {
$cost = $shipping['shipping_cost'];
}
any ideas?
$cost needs to be an array for it to store multiple costs unless you plan on using it in the for loop. ie..
$costTotal = 0;
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_shipping_cost");
foreach($query->row as $shipping) {
$costTotal += $shipping['shipping_cost'];
}
updated to reflect op comment.
Add the costs in your query:
$query = $this->db->query("SELECT SUM(shipping_cost) total_shipping FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
$cost = $query->row['total_shipping'];
done by using this code
$total = 0;
foreach ($this->cart->getProducts() as $product) {
$product_id = $product['product_id'];
$query = $this->db->query("SELECT shipping_cost FROM " . DB_PREFIX . "product_shipping_cost WHERE product_id='" . (int)$product_id . "'");
$total += $query->row['shipping_cost'];
$cost = $total;
}
but still will return an error if the shipping_cost for the product is not set in the database

opencart seo_url not working for categories

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
echo "SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'";
exit();
i have added the keyword for the category and the output for this echo is
SELECT * FROM oc_url_alias WHERE keyword = 'fashion'
i echoed this part in seo_url.php under controller/common.
this is working for products and information but not for category even when i run this output query in sql it gives me a single row with keyqord ='fashion'
how can the query be right, yet it gives 0 when keyword is for category.

Update one table where record found in another table

I am really struggling on how to achieve this and hope that someone can help me. I have the existing function and query:
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id)
}
This works, but it sets all products to Zero when I actually want it to only set products to Zero when that product exists in another table.
i.e., in explanatory terms:
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id AND product_id exists in CustomerProducts)
}
I am not au fait with joins, but I am not sure if I even need to use a join here as the query seems more simple than that.
Can anyone point me in the right direction?
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id ." AND product_id =(select DISTINCT(product_id) from CustomerProducts where product_id= $product_id)" )
}
This may work.
use this will work for you are not assigning db.product and make sure you write query in a string then execute.
And you see you query, by removing comments
public function setStockAndPrice($product_id) {
$query_string = "UPDATE " . DB_PREFIX . ".product SET quantity = '0', price = '0.00' WHERE product_id = '$product_id'";
// echo "Query : " . $query_string;
$query = $this->db->query($query_string);
}
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . '.product p, ' . DB_PREFIX . '.CustomerProducts cp SET p.quantity = 0, p.price = 0.00 WHERE p.product_id = ' . (int)$product_id . ' AND p.product_id = cp.product_id');
}

Categories