Jump to content

Create custom field in CustomerFormatter


Recommended Posts

Hello!

I want to create a radio field that would allow the user to choose in which group their account will be created (eg: individuals, professionals). And if the "Professionals" button is checked, the SIRET and Company fields will appear. I modified CustomerFormatter.php to get the radio field I want, but I don't know how to persist the information to database as well.

        $groups = Group::getGroups($this->language->id);
            $groupField = (new FormField())
                ->setName('id_default_group')
                ->setType('radio-buttons')
                ->setLabel(
                    $this->translator->trans(
                        'Statut',
                        [],
                        'Shop.Forms.Labels'
                    )
                );

            foreach ($groups as $group) {
                $groupField->addAvailableValue($group->id_group,$group->name);
            //    var_dump($group);
            }

I tried this, but Prestashop gives me an error: ContextErrorException
Notice: Trying to get property 'id_group' of non-object
However with a vardump, I do have an "id_group" output (I was inspired by "Gender")

Thanks in advance!

professionnal radio.png

Edited by beubz2
tried some new things (see edit history)
Link to comment
Share on other sites

I managed to display more or less what I wanted, but I don't think I followed the best practices. I still share my code and the result obtained, and leave the topic open if ever someone has a cleaner solution for me.

In classes/form/CustomerFormatter.php


        $groups = Group::getShopGroups($this->language->id);
        if ($genders->count() > 0) {
            $groupField = (new FormField())
                ->setName('id_default_group')
                ->setType('radio-buttons')
                ->setLabel(
                    $this->translator->trans(
                        'Statut',
                        [],
                        'Shop.Forms.Labels'
                    )
                );
            foreach ($groups as $group) {
                var_dump($group);
                $groupField->addAvailableValue($group->id);
            }
            $format[$groupField->getName()] = $groupField;
        }

And 

In classes/Group.php


    public static function getShopGroups($idLang = null)
    {
        if (null === $idLang) {
            $idLang = Context::getContext()->language->id;
        }
        $groups = new PrestaShopCollection('Group', $idLang);

        return $groups;
    }

 

Capture d’écran de 2021-12-06 12-38-15.png

Edited by beubz2
add image upload (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...