Mysql add value to the value opencart - php

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

Related

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.

Opencart sql query from unique table

I have a strongly modified opencart store.
I would like to display all products from subcategories on the main category pages. This is the half-done code from my product model file.
} else {
$cat_sql = "SELECT `category_id` FROM `" . DB_PREFIX . "category_path` WHERE `path_id` = '" . (int)$data['filter_category_id'] . "'";
$cat_query = $this->db->query($cat_sql);
if ($cat_query->num_rows) {
$sql .= " AND (p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
foreach($cat_query->rows as $cat) {
$sql .= " OR p2c.category_id = '" . (int)$cat['category_id'] . "'";
}
$sql .= ")";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
}
}
It's supposed to display those products.
But now I have another table for multiple parent categories.
My question is: how can I display the products from the subcategories, where I just have parent_id not path_id.
The multiple category table is simple.
category_id / parent_id
How can I modify the code above so that it displays all products from subcategories where they don't have path_id only parent_id?
You dont need to modify sql just pass extra parameter 'filter_sub_category' => true and it will list out all the product including child category
$filter_data = array(
'filter_category_id' => $category_id,
'filter_sub_category' => true
);
$results = $this->model_catalog_product->getProducts($filter_data);

OpenCart, display the total number of products

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'];

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');
}

Update multiple rows in Mysql

I have a sql table in my database "accounts" whose structure looks like this.
ItemNo CostPrice SellingPrice Discount(10%) Price(INR)
100 $100 $150 $135 Rs.7425
101 $200 $250 $225 Rs.10395
102 $150 $200 $180 Rs.7920
103 $500 $550 $495 Rs.25245
Here each column is dependent on the previous one.
SellingPrice = CostPrice + 50
Discount = SellingPrice + (0.1 * SellingPrice)
Price(INR) = SellingPrice * ($_Conversion_rate_to_Rs)
I need to calculate the SellingPrice and update it in the table.
Then I calculate the Discount and update the table
Then I calculate the Price(INR) and update the table.
I am trying the following code but it doesnt seem to be working.
$i = 0;
foreach($item_no as $item ){
mysql_query("UPDATE accounts SET SellingPrice = CostPrice + 50 WHERE item_no = '$item[$i]'") or die(mysql_error());
++$i;
}
Now after updating the SellingPrice I need to calculate the discount and update the database.
$i = 0;
foreach($item_no as $item ){
mysql_query("UPDATE accounts SET Discount = SellingPrice + (0.1 * SellingPrice) WHERE item_no = '$item[$i]'") or die(mysql_error());
++$i;
}
And so on..
My doubt may be quite simple but I tried a lot and couldn find a correct solution.
Can I know my mistakes and the correct solution ?
EDIT!
The way I edit multiple fields is with a custom update function. The function takes an associative array of "field"=>"new value". Here's the function:
function update($table,$values,$conditions){
$query = "UPDATE `" . $table . "` SET ";
foreach($values as $key => $value){
if($key == "password")
$query .= "`password` = PASSWORD('" . $mysqli->real_escape_string($value) . "'),";
else if(is_numeric($value))
$query .= "`" . $mysqli->real_escape_string($key) . "` = " . $mysqli->real_escape_string($value) . ",";
else
$query .= "`" . $mysqli->real_escape_string($key) . "` = '" . sanitize($value) . "',";
}
$query = substr($query,0,-1);
if(!is_string($conditions)){
$conditionStr = "";
foreach($conditions as $key => $value){
if($key == "password")
$conditionStr .= "`password` = PASSWORD('" . $mysqli->real_escape_string($value) . "') AND ";
else if(is_numeric($value))
$conditionStr .= "`" . $mysqli->real_escape_string($key) . "` = " . $mysqli->real_escape_string($value) . " AND ";
else
$conditionStr .= "`" . $mysqli->real_escape_string($key) . "` = '" . sanitize($value) . "' AND ";
}
$conditionStr .= substr($conditionStr,0,-4);
$conditions = $conditionStr;
}
$query .= " WHERE " . $conditions . " ;";
$mysqli->query($query);
}
You use it like this:
update("theTable",array("username"=>"whateverTheNewNameIs"),array("userID"=>55));
That updates userID=55 and makes the username something new. You can just expand the second array to update more things.
I think you a problem in your updates and your where clause
I'd make a variable for your new cost price,
$newSellingPrice = $item[$i]['sellingPrice'] + 50;
In your update you can use $newSellingPrice
Then in your where, you have a typo, you didn't use the $ to get the item array.
You'll need to leave the query string if you're using square bracket notation
"... WHERE item_no = " . $item[$i] . " ;";
To answer your question, if you want to update all fields in the table, there is no need to add a WHERE condition, you can just use:
UPDATE accounts SET SellingPrice = CostPrice + 50
and run only one query in total instead of one query per item.
However, all columns that depend on other columns really don't need to be separate columns, you would be a lot better of with a table with just 2 columns and calculate the prices as you need them. Now you have a lot of duplicate information as the cost price and the multipliers are the only values you really need to store.

Categories