Hi,
Searched through the forum but couldn't find a topic regarding this issue...
I need to get the total weight of an order in admin backend sales order. Went through sale order model and couldn't find a function which would handle this. Does anyone know if there's one I could use? Or do I need the create a new function to sale order model or modify the getOrder function? Idea anyone?
-Miguelito-
Searched through the forum but couldn't find a topic regarding this issue...
I need to get the total weight of an order in admin backend sales order. Went through sale order model and couldn't find a function which would handle this. Does anyone know if there's one I could use? Or do I need the create a new function to sale order model or modify the getOrder function? Idea anyone?
-Miguelito-
The Finnish OpenCart Forum
"Real programmers don't document. If it was hard to write, it should be hard to understand."
OK, solved this by creating a new public function getOrderWeight to sale order model and using that in the sale order controller
The Finnish OpenCart Forum
"Real programmers don't document. If it was hard to write, it should be hard to understand."
Why not share that code with us?Miguelito wrote:OK, solved this by creating a new public function getOrderWeight to sale order model and using that in the sale order controller
Norman in 't Veldt
Moderator OpenCart Forums
_________________ READ and Search BEFORE POSTING _________________
Our FREE search: Find your answer FAST!.
[How to] BTW + Verzend + betaal setup.
Sure
Added to /admin/model/sale/order.php
And used it in /admin/controller/sale/order.php
Added to /admin/model/sale/order.php
Code: Select all
public function getOrderWeight($order_id) {
$query = $this->db->query("SELECT SUM(p.weight * op.quantity) AS weight FROM " . DB_PREFIX . "order_product op LEFT JOIN " . DB_PREFIX . "product p ON op.product_id = p.product_id WHERE op.order_id = '" . (int)$order_id . "'");
if ($query->row['weight']) {
$weight = $query->row['weight'];
} else {
$weight = 1;
}
return $weight;
}
Code: Select all
$weight = array();
if (isset($this->request->get['order_id'])) {
$weight['weight'] = $this->model_sale_order->getOrderWeight($this->request->get['order_id']);
}
The Finnish OpenCart Forum
"Real programmers don't document. If it was hard to write, it should be hard to understand."
@Miguelito Thank you for sharing!
@tempe depending on which page you want to display the weight, you would add the code to the related function. So if you want to add the weight to the /index.php?route=sale/order/info page you would edit the info() function inside the /admin/controller/sale/order.php file.
I also added this line in the controller to make it work.
You will also have to edit the view for the page you want to have the weight be displayed on.
Add this line to display the weight:
@tempe depending on which page you want to display the weight, you would add the code to the related function. So if you want to add the weight to the /index.php?route=sale/order/info page you would edit the info() function inside the /admin/controller/sale/order.php file.
I also added this line in the controller to make it work.
Code: Select all
$this->data['weight'] = $weight['weight'];
Add this line to display the weight:
Code: Select all
<?php echo $weight; ?>
From what i can see no it is not possible. You have to edit the core files for it to work.j_v wrote:Hi,
Is there any way to add block to admin/index.php?route=sale/order/info&order_id=%someorder% without core files editing?
Best regards
It is a pity.nancymobley73 wrote:From what i can see no it is not possible. You have to edit the core files for it to work.j_v wrote:Hi,
Is there any way to add block to admin/index.php?route=sale/order/info&order_id=%someorder% without core files editing?
Best regards
Thank you for answer and happy New Year!
This will probably be what you want. Works in 1.5.3.
There are two problems with this method.
1. Weight Class - This does not take into account if multiple types of weight classes are used. Such as gram and kg. Rather than using a total SUM in the query, it may be best to loop each query returning the SUM of identical weight classes and then tally the weights using $this->weight->convert(). Using the weight_class_id from the "product" table and converting it to the weight class from the store configuration. (i.e. "SELECT SUM(p.weight * op.quantity) AS weight, p.weight_class_id FROM...")
2. Product Options - This method does not take into account weights from the product options. You also need to tally weights from the "product_option_value" table linked to "order_options."
1. Weight Class - This does not take into account if multiple types of weight classes are used. Such as gram and kg. Rather than using a total SUM in the query, it may be best to loop each query returning the SUM of identical weight classes and then tally the weights using $this->weight->convert(). Using the weight_class_id from the "product" table and converting it to the weight class from the store configuration. (i.e. "SELECT SUM(p.weight * op.quantity) AS weight, p.weight_class_id FROM...")
2. Product Options - This method does not take into account weights from the product options. You also need to tally weights from the "product_option_value" table linked to "order_options."
I have created the fallowing function:
It works with option but just not with weight classes.
Code: Select all
public function getOrderWeight($order_id){
$weight = 0;
$query_products = $this->db->query("SELECT op.product_id, op.order_product_id, op.quantity, p.weight FROM `" . DB_PREFIX . "order_product` op"
." LEFT JOIN `" . DB_PREFIX . "product` p ON (p.product_id = op.product_id)"
." WHERE order_id = '" . (int)$order_id . "'");
foreach($query_products->rows as $row){
$product_weight = (float)$row['weight'];
$query_options = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_option` oo"
." LEFT JOIN `" . DB_PREFIX . "product_option_value` pov ON (oo.product_option_value_id = pov.product_option_value_id)"
." WHERE oo.order_product_id = '" . (int)$row['order_product_id'] . "'");
// add weigth from option value if any
foreach($query_options->rows as $row_ov){
if ($row_ov['weight_prefix'] == '+') {
$product_weight += (float)$row_ov['weight'];
} elseif ($row_ov['weight_prefix'] == '-') {
$product_weight -= (float)$row_ov['weight'];
}
}
$weight += $product_weight * (int)$row['quantity'];
}
return $weight;
}
Who will interested for getOrderWeight function that will correctly count weight for product with option and also will check is product have to be shipped and will count weight according its weight class, look below (tested for 2.3.0.2):
public function getOrderWeight($order_id) {
$weight = 0;
$query_products = $this->db->query("SELECT op.product_id, op.order_product_id, op.quantity, p.weight, p.weight_class_id FROM " . DB_PREFIX . "order_product op LEFT JOIN " . DB_PREFIX . "product p ON (p.product_id = op.product_id) WHERE op.order_id = '" . (int)$order_id . "' AND p.shipping = 1");
foreach($query_products->rows as $product) {
$product_weight = (float)$product['weight'];
$query_options = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option oo LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (oo.product_option_value_id = pov.product_option_value_id) WHERE oo.order_product_id = '" . (int)$product['order_product_id'] . "'");
foreach($query_options->rows as $option){
if ($option['weight_prefix'] == '+') {
$product_weight += (float)$option['weight'];
} elseif ($option['weight_prefix'] == '-') {
$product_weight -= (float)$option['weight'];
}
}
$weight += $this->weight->convert(($product_weight * (int)$product['quantity']), $product['weight_class_id'], $this->config->get('config_weight_class_id'));
}
return $weight;
}
public function getOrderWeight($order_id) {
$weight = 0;
$query_products = $this->db->query("SELECT op.product_id, op.order_product_id, op.quantity, p.weight, p.weight_class_id FROM " . DB_PREFIX . "order_product op LEFT JOIN " . DB_PREFIX . "product p ON (p.product_id = op.product_id) WHERE op.order_id = '" . (int)$order_id . "' AND p.shipping = 1");
foreach($query_products->rows as $product) {
$product_weight = (float)$product['weight'];
$query_options = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option oo LEFT JOIN " . DB_PREFIX . "product_option_value pov ON (oo.product_option_value_id = pov.product_option_value_id) WHERE oo.order_product_id = '" . (int)$product['order_product_id'] . "'");
foreach($query_options->rows as $option){
if ($option['weight_prefix'] == '+') {
$product_weight += (float)$option['weight'];
} elseif ($option['weight_prefix'] == '-') {
$product_weight -= (float)$option['weight'];
}
}
$weight += $this->weight->convert(($product_weight * (int)$product['quantity']), $product['weight_class_id'], $this->config->get('config_weight_class_id'));
}
return $weight;
}
Who is online
Users browsing this forum: No registered users and 64 guests