phpprestashopadminproduct-quantity

Prestashop Add custom column called "number of products", displaying quantity ordered from "ps_order" table


I am trying to find a way to display product ordered quantity in Orders Page, in back-end of a Prestashop v.1.6.1.9 installation.

I already managed to add 2 custom columns by overriding AdminOrdersController.php. I have added phone_mobile and custom notes in this manner:

$this->fields_list['phone_mobile'] = array(
        'title' => $this->l('Phone Number')
    );

$this->fields_list['note'] = array(
        'title' => $this->l('Notes')
    );

Any way I can override this file to show the quantity ordered?


Solution

  • First of all let me clear one thing; quantity ordered is not getting stored in {DB_PREFIX}order table; it is stored in {DB_PREFIX}order_detail table.

    To add total_qty total quantities ordered you need to get quantity from {DB_PREFIX}order_detail table and to achieve this you can do below things in your override.

    <?php
    /**
     * @override AdminOrdersController
     */
    
    class AdminOrdersController extends AdminOrdersControllerCore
    {
        public function __construct()
        {
            parent::__construct();        
            $this->_select .= ', (SELECT SUM(od.product_quantity) FROM `'._DB_PREFIX_.'order_detail` od WHERE od.id_order = a.id_order GROUP BY od.id_order) as total_qty';
    
            $this->fields_list = array_merge($this->fields_list, array(
                'total_qty' => array(
                    'title' => $this->l('Number of products'),
                    'havingFilter' => true,
                ),
            ));
        }
    }
    

    You can add your fields like phone_mobile and custom_notes accordingly.

    Hope it helps!