Page 1 of 1

Order total weight to admin's sales order

Posted: Sat Dec 25, 2010 9:45 pm
by Miguelito
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-

Re: Order total weight to admin's sales order

Posted: Sun Dec 26, 2010 6:26 pm
by Miguelito
OK, solved this by creating a new public function getOrderWeight to sale order model and using that in the sale order controller :)

Re: Order total weight to admin's sales order

Posted: Sun Dec 26, 2010 8:26 pm
by i2Paq
Miguelito wrote:OK, solved this by creating a new public function getOrderWeight to sale order model and using that in the sale order controller :)
Why not share that code with us?

Re: Order total weight to admin's sales order

Posted: Mon Dec 27, 2010 12:44 am
by Miguelito
Sure :)

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;
	}
And used it in /admin/controller/sale/order.php

Code: Select all

$weight = array();
                        if (isset($this->request->get['order_id'])) {
			$weight['weight'] = $this->model_sale_order->getOrderWeight($this->request->get['order_id']);
		        }

Re: Order total weight to admin's sales order

Posted: Mon Apr 04, 2011 7:30 am
by tempe
tried this one but confuse where to place that code, already tried to place them somewhere but not working

Re: Order total weight to admin's sales order

Posted: Wed Sep 28, 2011 1:13 am
by ddowdall
@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.

Code: Select all

$this->data['weight'] = $weight['weight'];
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:

Code: Select all

<?php echo $weight; ?>

Re: Order total weight to admin's sales order

Posted: Thu Dec 22, 2011 3:59 pm
by j_v
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

Re: Order total weight to admin's sales order

Posted: Wed Dec 28, 2011 10:41 pm
by nancymobley73
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
From what i can see no it is not possible. You have to edit the core files for it to work.

Re: Order total weight to admin's sales order

Posted: Thu Jan 05, 2012 10:03 pm
by j_v
nancymobley73 wrote:
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
From what i can see no it is not possible. You have to edit the core files for it to work.
It is a pity.

Thank you for answer and happy New Year!

Re: Order total weight to admin's sales order

Posted: Wed Nov 21, 2012 10:35 pm
by joshgeake
This will probably be what you want. Works in 1.5.3.

Re: Order total weight to admin's sales order

Posted: Sun Jan 13, 2013 8:34 pm
by planetdrop
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."

Re: Order total weight to admin's sales order

Posted: Fri May 09, 2014 12:36 am
by thbr02
I think this is a very usefull extension, but I really would like to include the product option weight as well. Any suggestions how I can change the code above?

Re: Order total weight to admin's sales order

Posted: Sat May 10, 2014 11:17 pm
by thbr02
Thought I've sovled it, but I didn't ???. Would appreciate some help.

Re: Order total weight to admin's sales order

Posted: Thu Apr 26, 2018 12:47 pm
by chokybp
I have created the fallowing function:

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;
    }
It works with option but just not with weight classes.

Re: Order total weight to admin's sales order

Posted: Mon Sep 09, 2019 9:13 pm
by lialkz
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;
}