Jump to content

Mandatory company and ID in registration form PS 1.7


Recommended Posts

Hi!

 

I have tried several things to get the fields for Company and Identification (ID) mandatory in the customer registration form. I use PS 1.7.2.0 with B2B enabled.

 

Things I have done:

- Copied Address.php to override/classes using 'required' => true at the corresponding fields.

- Used the "Mandatory Fields"-section in Customers > Addresses.

- VAT number and company are listed under mandatory fields in International > Countries > [My country] > Edit.

 

Nothing helps.

 

Would appreciate any help! :-)

 

// Andreas

Link to comment
Share on other sites

I will give a partial answer myself :-)

 

I tried again using the correct settings here:

- Used the "Mandatory Fields"-section in Customers > Addresses.

- VAT number and company are listed under mandatory fields in International > Countries > [My country] > Edit.

 

Now I see that the fields are mandatory - but ONLY in step 2 of the checkout process.

Link to comment
Share on other sites

I will give a partial answer myself :-)

 

I tried again using the correct settings here:

- Used the "Mandatory Fields"-section in Customers > Addresses.

- VAT number and company are listed under mandatory fields in International > Countries > [My country] > Edit.

 

Now I see that the fields are mandatory - but ONLY in step 2 of the checkout process.

 

I advise switching to one page check out.  I recommend using a paid 3rd party OPC module in general.

Link to comment
Share on other sites

  • 4 months later...

Did anyone succeed at making any fields other than the standard ones required? I am making the same attempts OP is doing in the previous posts but it seems undoable. I remember this being an issue also in version 1.6, time to fix it perhaps? Most people would argue it is a pretty basic functionality in a registration form to select which fields are supposed to be mandatory. No?

Link to comment
Share on other sites

  • 2 years later...
  • 4 years later...

The hook additionalCustomerFormFields is sent the array of the field definitions, so you can manipulate them in there and never have to worry about files getting updated and having to mess with core changes and overrides. There's an equivalent for most forms.

Here's an example module that uses this process to change a field name (but you can do the same with the setRequired() method too, of course). This specific module is intended to target Prestashop 8.0.0 onwards and PHP 8.1. May need to be modified for earlier versions, but the principle is supported from 1.7 onwards. You may need to remove declare(strict_types=1) and the function return types if you're using an older version of PHP.

Documentation can be found here (PrestaShop 😎.

 

<?php
/** 
 * Module Name: formlabelmangler
 * Description: Example code to show how to modify field labels
 * Version: 8.0.0
 * @author: Paul Campbell [https://www.prestashop.com/forums/profile/11264-paul-c/] 
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
declare(strict_types=1);

if (!defined('_PS_VERSION_')) {
    exit;
}

class formlabelmangler extends Module
{   
    /** @var array */
    private $hooks;

    /**
     * @param string|null $name (Deprecated parameter)
     * @param Context|null $context
     * @return void
     */
    public function __construct()
    {
        $this->name = 'formlabelmangler';
        $this->tab = 'front_office_features';
        $this->author = 'PaulC';
        $this->version = '8.0.0';

        parent::__construct();

        $this->displayName = $this->trans('Fun with forms and hooks.', [], 'Modules.formlabelmangler.Admin');
        $this->description = $this->trans('Modify field names on customer registration form.', [], 'Modules.formlabelmangler.Admin');
        $this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => _PS_VERSION_);

        // Hooks to install to
        $this->hooks =  [
            'additionalCustomerFormFields'
        ];
    }

    /**
     * Install and hook module.
     * @return bool True on success, False if an error is encountered
     */
    public function install()
    {
        return 
            parent::install() &&
            $this->registerHook($this->hooks);
    }

    /**
     * Respond whether the module uses the new translation system.
     * 
     * @return bool
     */
    public function isUsingNewTranslationSystem(): bool
    {
        return true;
    }

    /**
     * Inject additional customer registration fields and/or manipulate existing
     * Hook::exec('additionalCustomerFormFields', ['fields' => &$format], null, true)
     * @see https://github.com/PrestaShop/PrestaShop/blob/develop/classes/form/FormField.php
     * 
     * @param array FormField &$arguments['fields'] any additional data
     * @return array The field definitions to be rendered
     * 
     */
    public function hookAdditionalCustomerFormFields(array $arguments) : array
    {
        // The current fields which are set can be retrieved with the following
        // since the data is passed by reference.
        $fields = $arguments['fields'];

        // Uncomment the following if you want to record the current field details in the
        // PHP error log for reference.
        
        //error_log('We got ourselves some fields to play in: '. print_r($fields, true));

        // In this example we want to spruce up the 'email' field and call it "Email Address"
        // instead of just plain old "Email".
        foreach ($fields as $field) {
            if ($field->getName() == 'email') {
                $field->setLabel(
                    $this->trans(
                        'Email Address', [], 'Shop.Forms.Labels'
                    ));
            }
        }

        // This is where you would return any additional fields you want to create
        return [];
    }
}

 

Edited by Paul C
added link to docs (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...