Display nb of orders on list of customers

In this guide i want to show you how to modify your shop back office.I will describe step by step how to display number of customers' orders that will appear near customer on list of customers that is available under customers > customers section in your BO. Well, case requires modification of PrestaShop controller called AdminCustomersController - and i will describe whole process below. Let's go!

 

You can see new field "orders" that displays number of all orders that customer placed in your shop

nb of customers orders

 

How to modify AdminCustomersController ?

To make feature like i show on image above please open file: /controllers/admin/AdminCustomersController.php. Find there line that defines what fields controller will get from database, it starts with $this->_select and it is somewhere near 161 line. At the end of the mysql query add code:

(SELECT COUNT(*) FROM '._DB_PREFIX_.'orders o where o.valid =1 AND (o.id_customer = a.id_customer)) AS nb_of_orders

the whole code of "select" should look like:

        $this->_select = '
        a.date_add, gl.name as title, (
            SELECT SUM(total_paid_real / conversion_rate)
            FROM '._DB_PREFIX_.'orders o
            WHERE o.id_customer = a.id_customer
            '.Shop::addSqlRestriction(Shop::SHARE_ORDER, 'o').'
            AND o.valid = 1
        ) as total_spent, (
            SELECT c.date_add FROM '._DB_PREFIX_.'guest g
            LEFT JOIN '._DB_PREFIX_.'connections c ON c.id_guest = g.id_guest
            WHERE g.id_customer = a.id_customer
            ORDER BY c.date_add DESC
            LIMIT 1
        ) as connect,
        (SELECT COUNT(*) FROM '._DB_PREFIX_.'orders o where o.valid =1 AND (o.id_customer = a.id_customer)) AS nb_of_orders
        ';

Ok, so the query to get number of orders from database is ready. Now it's time to display it. In this case we will have to modify helper list definition. Helper list is a feature available in prestashop that allows to build list of things available in your shop like products, orders, customers etc. You can do it in the same file: AdminCustomersController.php. Find there line that starts with $this->fields_list - this is definition of what controller will display inside the list of customers. At the end please add code that will display our field from mysql query named nb_of_orders:

            'nb_of_orders' => array(
                'title' => $this->l('Orders')
            ),

the full code of fields_list definition should look like:

        $this->fields_list = array(
            'id_customer' => array(
                'title' => $this->l('ID'),
                'align' => 'text-center',
                'class' => 'fixed-width-xs'
            ),
            'title' => array(
                'title' => $this->l('Social title'),
                'filter_key' => 'a!id_gender',
                'type' => 'select',
                'list' => $titles_array,
                'filter_type' => 'int',
                'order_key' => 'gl!name'
            ),
            'firstname' => array(
                'title' => $this->l('First name')
            ),
            'lastname' => array(
                'title' => $this->l('Last name')
            ),
            'email' => array(
                'title' => $this->l('Email address')
            ),
            'nb_of_orders' => array(
                'title' => $this->l('Orders')
            ),
        );


Now you can save changes in this file. After refresh your back office "customers > customers" section you should see new column with "orders" title - it will show the total number of orders that customer placed in your store.

 

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim