Search code examples
phpprestashopprestashop-1.6

PrestaShop: inner join to show customer name in a list


I'm making a PrestaShop 1.6 plugin where I can configure discount by costumers, my table below:

create table ps_discountbycustomer
(
    id_discountbycustomer int not null auto_increment
        primary key,
    id_customer int not null,
    date_start datetime not null,
    date_end datetime not null,
    use_percentage bit not null,
    percentage_order float not null,
    percentage_shipping float not null,
    value_order float not null,
    value_shipping float not null
);

My module list: enter image description here

How I'm making my controller class:

class AdminDiscountbycustomerController extends AdminController
{

    public function __construct()
    {
        $this->bootstrap  = true;
        $this->table = 'discountbycustomer';
        $this->className = 'DiscountbycustomerModel';
        $this->identifier = 'id_discountbycustomer';

        parent::__construct();

        $this->lang = false;

        $this->_join = 'INNER JOIN '._DB_PREFIX_.'customer c ON (a.id_customer = c.id_customer)';

        // Building the list of records stored within the "test" table
        $this->fields_list = array(
//            'id_discountbycustomer' => [
//                'title' => $this->l('ID'),
//                'align' => 'center',
//                'width' => 25
//            ],
            'id_customer' => [
                'title' => $this->l('Customer'),
                'width' => 'auto'
            ],
            'date_start' => [
                'title' => $this->l('Starts'),
                'width' => 'auto',
                'type' => 'date'
            ],
            'date_end' => [
                'title' => $this->l('Ends'),
                'width' => 'auto',
                'type' => 'date'
            ],
            'use_percentage'   => [
                'title'  => $this->l('Use Percentage'),
                'active' => 'use_percentage',
                'align'  => 'text-center',
                'class'  => 'fixed-width-sm'
            ],
            'percentage_order'   => [
                'title' => $this->l('Order %'),
                'width' => 'auto',
                'type' => 'float'
            ],
            'percentage_shipping'   => [
                'title' => $this->l('Shipping %'),
                'width' => 'auto',
                'type' => 'float'
            ],
            'value_order'   => [
                'title' => $this->l('Order %'),
                'width' => 'auto',
                'type' => 'float'
            ],
            'value_shipping'   => [
                'title' => $this->l('Shipping %'),
                'width' => 'auto',
                'type' => 'float'
            ],
        );

        // This adds a multiple deletion button
        $this->bulk_actions = array(
            'delete' => array(
                'text' => $this->l('Delete selected'),
                'confirm' => $this->l('Delete selected items?')
            )
        );

        parent::__construct();
    }
}

My problem is that I don't know how I can add the costumer column in the $this->fields_list array so I can get the costumer's name displayed in the list.

How can I do that? I'm using PrestaShop 1.6.1.12. Thanks for any help


Solution

  • You need to add the select to concat the name

    $this->_select = 'a.*, CONCAT(c.`firstname`, \' \', c.`lastname`) AS `customer`';
    

    then, instead of the field id_customer use customer:

    'customer' => [
        'title' => $this->l('Customer'),
        'width' => 'auto'
    ],