Berna
Berna

Reputation: 131

Add new field to Customer Group in Prestashop

I'm trying to add a custom field to the customer groups in Prestashop 1.6, editable from the admin area. I've tried various solutions, but the approach that worked best is the one described in this guide: http://nemops.com/extending-prestashop-objects/. I'd done the same before reading it, but it seems that this guy got it working this way.

What I've done is, basically, add a new column in the ps_group table, making it not null (the new field is mandatory for all groups).

Then, in the file classes/Group.php I've edited the $definitionarray. (I know I shouldn't edit the core. I'll externalize all of this once I check it works).

public static $definition = array(
    'table' => 'group',
    'primary' => 'id_group',
    'multilang' => true,
    'fields' => array(
        'reduction' =>              array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
        'price_display_method' =>   array('type' => self::TYPE_INT, 'validate' => 'isPriceDisplayMethod', 'required' => true),
        'show_prices' =>            array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
        'date_add' =>               array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
        'date_upd' =>               array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
        'provinces' =>              array('type' => self::TYPE_STRING, 'required' => true, 'size' => 200),

        // Lang fields
        'name' =>                   array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
    ),
);

The 'provinces' key is my addition.

Then I edited the file controllers/admin/AdminGroupsController.php to add the new field in the admin area, so it would be editable. In the method renderForm() I added a new child of 'input' of the $this->fields_form array. My modification is the following:

array(
    'type' => 'select',
    'label' => $this->l('Provinces'),
    'name' => 'provinces',
    'required' => 'true',
    'col' => 3,
    'hint' => $this->l('The provinces belonging to this customer group.'),
    'options' => array(
        'query' => array(
            array(
                'id_method' => '1',
                'name' => 'Option 1'
            ),
            array(
                'id_method' => '2',
                'name' => 'Option 2'
            )
        ),
        'id' => 'id_method',
        'name' => 'name'
    )
),

I understand that naming equally the database column and the field will cause the value in this field to be saved, just as the rest of fields. But it doesn't happen. When I edit the group, make some change and save it, the following error appears at the top of the window, near an exclamation mark:

Property Group->provinces is empty

The POST form, nevertheless, is sent correctly, and my data is passed just fine, just as the rest of fields.

Is there something I'm missing here? I think these are all the necessary steps to add a new field to any object (except products, which works differently).

Is my code wrong at some point? Or else, how can I add a new field to the customer groups?

Thank you very much :)

Upvotes: 1

Views: 1824

Answers (1)

Cyrille Armanger
Cyrille Armanger

Reputation: 759

What you are missing is to add the property $provinces in classes/Group.php, see the example below:

class GroupCore extends ObjectModel
{
    public $id;

    /** @var string Lastname */
    public $name;

    /** @var string Reduction */
    public $reduction;

    /** @var int Price display method (tax inc/tax exc) */
    public $price_display_method;

    /** @var bool Show prices */
    public $show_prices = 1;

    /** @var string Object creation date */
    public $date_add;

    /** @var string Object last modification date */
    public $date_upd;

    /** @var string Province */
    public $provinces;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(

Additionally, you can find below several ways of optimization of your code:

  • Use an Integer as provinces value if possible, this is safer and easier to use. You will need to update your code accordingly (the definition, database, form).
  • Do not directly modify the files of PrestaShop core. This will prevent any further update of PrestaShop and should be avoided. You can easily override the core behavior by using the override folder in the root of the PrestaShop site then controllers then admin. There, create a new file AdminGroupsController.php as this is what you want to edit. In this file, you can override the AdminGroupsController behavior by creating a new class; in your case:

     class AdminGroupsController extends AdminGroupsControllerCore
     {
         /* Your code here */
     }
    

Then copy/paste and edit the methods you need to edit. This is also the place where you should add the property $provinces.

Do not forget to delete the file class_index.php in the cache folder located at the root of your PrestaShop site once you have saved your modifications.

Upvotes: 2

Related Questions