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