Jump to content

Saving a new field I added to "customer registration form" with my module on customer creation


Recommended Posts

Hello!

I represent a team of a developer and a content manager. We are using PrestaShop v1.7.4.4 to develop and support a shop for a client.

The context

My goal currently is adding a custom field to the customer registration form.
Customer should be able to fill the form as usual with option to specify their phone number.
The phone should be saved somewhere in the database, preferably in it's own table with a key pointing to a row in customers table.
I'd also like to see that information in the back office somewhere.

There seem to be a couple modules I found that would work for this purpose.

Registration Fields
Custom Fields Module
Both of them are priced the same (€39). That's pretty expensive for us as we've bought an entire theme for this shop and it costed us €70 already, as a developer I'd rather write my own module.
The first one comes from a website I'm not sure I can trust, second seems to be the official prestashop addons marketplace, but the compatibility goes up to prestashop v1.6.1.23.

So another option would be to use a quick solution like modifying the core file as described in this tutorial ADDITIONAL FIELDS FOR REGISTRATION FORM PRESTASHOP 1.7 

I will probably take this route as the last resort.

So far my choice is to create a module for this purpose, and I've had some progress with this.

What I did

Used module generator as described in the docs Modules > Getting started
    this provided me with a working module template I could upload to the shop and see in the module list, nice starting point!
Added 'additionalCustomerFormFields' hook to display my new field in the form
    another successful step, the field is displayed as expected
Added 'validateCustomerFormFields' hook to validate it
    using ->addError method results in visible error on the form so it's working too

The issue
Next thing I tried was using 'actionCustomerAccountAdd' hook to acquire the new customer and save into database the field information provided.

According to the docs at List of hooks, 'actionCustomerAccountAdd' hook should give the following array as a parameter 

array(
  '_POST' => (array) $_POST,
  'newCustomer' => (object) Customer object
);

Again, according to aforementioned docs this hook should be triggered in /classes/form/CustomerPersister.php

when I check the file however it only seems to provide the 'newCustomer', but no '_POST'

Hook::exec('actionCustomerAccountAdd', array(
    'newCustomer' => $customer,
));

logging the argument I get in my hookActionCustomerAccountAdd method gives me an array with following members: newCustomer, cookie, cart, and altern.

That's surprisingly more than what 'create' method in 'CustomerPersister.php' provides, but there is no '_POST' that I can see anyway.

here is body of the function I use to log

public function hookActionCustomerAccountAdd(...$args)
{
    file_put_contents(DIRNAME(__FILE__).'/test_log.txt', var_export($args, true));
}

I'm attaching the full output of it for inspection.

Current state of things

It's cool that my module is able to see the contents of the field at the point of validation, it can also get new customer when it's created, but there seems to be no way to get the field and new customer id. If the '_POST' data was provided as described in the docs, I assume that would allow to get the form posed as-is, including my custom field, but that doesn't seem to work.

In hopes that both 'validateCustomerFormFields' and 'actionCustomerAccountAdd' hooks are used in the same run I could try to set some sort of global variable just to save the value, but that looks uglier than modifying core files.

Another thing I could do is assume the form is valid when the phone is valid (very optimistic guess), precalculate the new user's id from mysql's atoincrement feature and save the phone early, doesn't look like a good solution either.

Please tell me of a hook I'm missing or any other methods of getting the value of my custom field after the entire form is considered valid and new user's id is available.

test_log.txt

Link to comment
Share on other sites

  • 7 months later...
On 2/15/2019 at 4:10 PM, hhennes said:

Hi,


You could access to GET and POST vars in your hook just using the function :


Tools::getValue('your_key');

Which will return $_GET['your_key'] OR $_POST['your_key']

Regards,

Hi, please help me!

This if tpl for hook

<label class="col-md-3 form-control-label required">
    Mobile numberd
</label>
<div class="col-md-6">
        <input class="form-control" name="customer_number" id="customer_number" type="text" required="">
</div>
<div class="col-md-3 form-control-comment">
</div>
    public function hookDisplayCustomerAccountForm()
    {            
        $customer_number = Tools::getValue('customer_number');
        Configuration::updateValue('customer_number', $customer_number);
        return $this->display(__FILE__, 'numberForm.tpl');
    }

    public function hookActionCustomerAccountAdd()
    {
        $cust = $this->context->customer;
        $customer_number = Configuration::get('customer_number');
        Configuration::updateValue('customer_number', $customer_number);
        $query = 'UPDATE `' . _DB_PREFIX_ . 'customer` 
                  SET `customer_number` = "' . pSQL($customer_number).'" 
                  WHERE `id_customer` = "'.(int)$cust->id.'"';
          
        $res = Db::getInstance()->execute($query);
        Configuration::deleteByName('customer_number');
    }

Above is hook code, it doesn't write value to Configuration table. Hooks are registrated and presta shop is the latest 1.7.6.

Am i doing something wrong or is this some bug?

Link to comment
Share on other sites

Hi,
I guess that your approach is not good.
When the hook hookDisplayCustomerAccountForm() is called the value of your field customer_number is not defined.
( As the customer has not fill the field yet )
So you should use $customer_number = Tools::getValue('customer_number'); in the hookActionCustomerAccountAdd() to get the value posted by the submit form.
By the what's the utility of the configuration ?

Regard,

Link to comment
Share on other sites

4 hours ago, hhennes said:

Hi,
I guess that your approach is not good.
When the hook hookDisplayCustomerAccountForm() is called the value of your field customer_number is not defined.
( As the customer has not fill the field yet )
So you should use $customer_number = Tools::getValue('customer_number'); in the hookActionCustomerAccountAdd() to get the value posted by the submit form.
By the what's the utility of the configuration ?

Regard,

Thank you, sir, this worked I misunderstood these hooks, as for configuration I used it for testing to make sure i haven't made any mistakes in writing or processing data just pass it to a database to see what I got.

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...