combine left join into my queries but failed - php

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 . "'");

Related

Opencart 3.x: Change manual invoice in adm let disabled products to be added

hello i nee dto customize my opencart manual invoice in admin.
id like to add disbaled products and enabled to my invoice. but only admin in administration can make it.
i tried to change this file:
system/library/cart/cart.php and in line 41
i replaced the line 41 query to
if (isset($this->session->data['api_id'])) {
$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store p2s LEFT JOIN " . DB_PREFIX . "product p ON (p2s.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2s.product_id = '" . (int)$cart['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
} else {
$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_store p2s LEFT JOIN " . DB_PREFIX . "product p ON (p2s.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND p2s.product_id = '" . (int)$cart['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'");
}
but its keepeing blocking me to add disabled products on admin manual invoice cart.
To check if admin is logged in use condition
if (isset($this->session->data['user_id']) && !empty($this->session- >data['user_token'])) {
// admin sql
}else{
// stock sql
}

How to get another column values from MySQL Query in opencart3 admin panel?

The code below uses 2 tables "category_path" and "category_description" to get id=>name of all categories and sub-categories. Im bad at mySql, so I would appreciate if you help me.
In this function I need to also get values of 'cat_name' column FROM THE OTHER (third) table named 'category'
https://i.stack.imgur.com/1PC4A.jpg
public function getCategories($data = array()) {
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id, c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";
You are already using that category table in the first join and you gave it an alias of c1 so
SELECT cp.category_id AS category_id, c1.cat_name,
. . .
. . .

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 select something as where is not null

I'm quite new at this, so I have question about MySQL. I have query in openCart:
$sql = "SELECT cp.category_id AS category_id,
GROUP_CONCAT(cd1.name
ORDER BY cp.level SEPARATOR ' > ') AS name,
c1.parent_id,
c1.sort_order
FROM " . DB_PREFIX . "category_path cp
LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id)
LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id)
LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id)
WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND cd2.language_id = '" . (int)$this->config->get('config_language_id') ."'";
In MySQL I added new column "show_menu" (it can be NULL or 1) and now I want to change my query, that It will return only categories in where show_menu = 1
As I understand I need something like: WHERE show_menu IS 1.
Maybe anyone can help where to put it? because I tried, but no luck...
Use show_menu = 1 or show_menu IS NOT NULL
You just can't use the '=' operator with a null value.
Just add the following to your WHERE clause:
... AND show_menu = 1
Just Like
SELECT * FROM tablename
WHERE conditions_from_your_existing_query
AND show_menu = 1
You can use show_menu = 1 or you can use show_menu is not null. Either way will work.

Opencart: Product Name Print Several times, How to fix.?

i had added the following Opencart module for my order report list...
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=3597&filter_search=order%20list%20filter%20model&page=4
I have problems with the column "Products". If there are more than one option the products name prints several times. So if I got a product with three options the product name prints three times. Is there any way to fix this problem?
i want print product name and model number only once, any idea.?
i will attach the results what i got now...
this is my sql query...
public function getOrders($data = array()) {
$sql = "select o.order_id,o.email,o.telephone,CONCAT(o.shipping_address_1, ' ', o.shipping_address_2) AS address,CONCAT(o.firstname, ' ', o.lastname) AS customer,o.payment_zone AS state,o.payment_address_2 AS block, o.payment_address_1 AS address,o.payment_postcode AS postcode,(SELECT os.name FROM " . DB_PREFIX . "order_status os WHERE os.order_status_id = o.order_status_id AND os.language_id = '" . (int)$this->config->get('config_language_id') . "') AS status,o.payment_city AS city,GROUP_CONCAT(pd.name) AS pdtname,GROUP_CONCAT(op.model) AS model,o.date_added,sum(op.quantity) AS quantity,GROUP_CONCAT(opt.value ) AS options, GROUP_CONCAT(opt.order_product_id ) AS ordprdid,GROUP_CONCAT(op.order_product_id ) AS optprdid, GROUP_CONCAT(op.quantity) AS opquantity from `" . DB_PREFIX . "order` o LEFT JOIN " . DB_PREFIX . "order_product op ON (op.order_id = o.order_id) LEFT JOIN " . DB_PREFIX . "product_description pd ON (pd.product_id = op.product_id and pd.language_id = '" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "order_option opt ON (opt.order_product_id = op.order_product_id) ";
Product Name = GROUP_CONCAT(pd.name) AS pdtname,
Add DISTINCT
GROUP_CONCAT(DISTINCT pd.name)

Categories