Jump to content

required field multistore prestashop


girl_dev

Recommended Posts

Unfortunately, the required fields are not multishop, so you can't have fields required for one shop, but not another. You'd have to hack a solution by finding the code that checks required fields and add some if statements that check the current shop ID.

  • Like 1
Link to comment
Share on other sites

finally I find the solution :
in controllers/AuthController.php
in the function  public function postProcess
I added: $id_shop = $this->context->shop->id;
then in this bloc :if (Tools::isSubmit('submitAccount') || Tools::isSubmit('submitGuestAccount')) {
I added:
if($id_shop==3){
         if (Tools::getValue('company') == '') {
                    $this->errors[] = Tools::displayError('company is required.');
         }
            if (Tools::getValue('vatnumber') == '') {
                    $this->errors[] = Tools::displayError('vatnumber is required.');
           }
            }

I hope it helps someone else
 

  • Like 1
Link to comment
Share on other sites

  • 8 years later...

 

On 9/8/2016 at 10:33 AM, girl_dev said:

finally I find the solution :
in controllers/AuthController.php
in the function  public function postProcess
I added: $id_shop = $this->context->shop->id;
then in this bloc :if (Tools::isSubmit('submitAccount') || Tools::isSubmit('submitGuestAccount')) {
I added:
if($id_shop==3){
         if (Tools::getValue('company') == '') {
                    $this->errors[] = Tools::displayError('company is required.');
         }
            if (Tools::getValue('vatnumber') == '') {
                    $this->errors[] = Tools::displayError('vatnumber is required.');
           }
            }

I hope it helps someone else
 

2025 and still the same with 8.2 that's crazy.

What did you exactly do? (if you remember) 😁 because i am not pro dev and really need this to be removed on my non B2B shops.

Link to comment
Share on other sites

Posted (edited)

ok so i find the solution for prestashop 8.2 for anyone who needs it.

first of all make sure both company and VAT number are unchecked in back office (this code will do it just for a specific store)

In controllers/front/RegistrationController.php

change this 

    public function initContent()
    {
        $register_form = $this
            ->makeCustomerForm()
            ->setGuestAllowed(false)
            ->fillWith(Tools::getAllValues());

        // If registration form was submitted
        if (Tools::isSubmit('submitCreate')) {
            $hookResult = array_reduce(
                Hook::exec('actionSubmitAccountBefore', [], null, true),
                function ($carry, $item) {
                    return $carry && $item;
                },
                true
            );

            // If no problem occured in the hook, let's get the user redirected
            if ($hookResult && $register_form->submit() && !$this->ajax) {
                // First option - redirect the customer to desired URL specified in 'back' parameter
                // Before that, we need to check if 'back' is legit URL that is on OUR domain, with the right protocol
                $back = rawurldecode(Tools::getValue('back'));
                if (Tools::urlBelongsToShop($back)) {
                    return $this->redirectWithNotifications($back);
                }

                // Second option - we will redirect him to authRedirection if set
                if ($this->authRedirection) {
                    return $this->redirectWithNotifications($this->authRedirection);
                }

                // Third option - we will redirect him to home URL
                return $this->redirectWithNotifications(__PS_BASE_URI__);
            }
        }

        $this->context->smarty->assign([
            'register_form' => $register_form->getProxy(),
            'hook_create_account_top' => Hook::exec('displayCustomerAccountFormTop'),
        ]);
        $this->setTemplate('customer/registration');

        parent::initContent();
    }

by this but make sure you add the id of your store correctly

public function initContent()
{
    $register_form = $this
        ->makeCustomerForm()
        ->setGuestAllowed(false)
        ->fillWith(Tools::getAllValues());

    $id_shop = $this->context->shop->id;

    // If registration form was submitted
    if (Tools::isSubmit('submitCreate')) {
        //  Custom validation for shop ID 3
        if ($id_shop == 3) {
            $company = Tools::getValue('company');
            $vat = Tools::getValue('vat_number'); // PrestaShop uses "vat_number" internally

            if (empty($company)) {
                $register_form->getField('company')->addError(
                    $this->trans('Company is required.', [], 'Shop.Notifications.Error')
                );
            }

            if (empty($vat)) {
                $register_form->getField('vat_number')->addError(
                    $this->trans('VAT number is required.', [], 'Shop.Notifications.Error')
                );
            }
        }

        // Keep the hook and original logic
        $hookResult = array_reduce(
            Hook::exec('actionSubmitAccountBefore', [], null, true),
            function ($carry, $item) {
                return $carry && $item;
            },
            true
        );

        if ($hookResult && $register_form->submit() && !$this->ajax) {
            $back = rawurldecode(Tools::getValue('back'));
            if (Tools::urlBelongsToShop($back)) {
                return $this->redirectWithNotifications($back);
            }

            if ($this->authRedirection) {
                return $this->redirectWithNotifications($this->authRedirection);
            }

            return $this->redirectWithNotifications(__PS_BASE_URI__);
        }
    }

    $this->context->smarty->assign([
        'register_form' => $register_form->getProxy(),
        'hook_create_account_top' => Hook::exec('displayCustomerAccountFormTop'),
    ]);

    $this->setTemplate('customer/registration');

    parent::initContent();
}

😁😁😁😁

Edited by mktm20111 (see edit history)
  • Like 1
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...