PrestaShop Connector - Create Customer (OptA4SNewCustomers)
This function handles the creation or update of customer records in PrestaShop, triggered by data received from the App4Sales platform. It processes incoming customer information, validates essential fields, performs duplicate checks, maps data to PrestaShop's customer and address structures, and then submits the data to the PrestaShop API. It supports both creating new customers and updating existing ones based on a unique identifier.
Trigger & Permissions
The customer creation flow is initiated by an internal request from the App4Sales platform (e.g., a new customer submission from the mobile app or a portal request). It requires the CreateCustomer feature flag to be enabled within the connector's configuration settings (ConnectorInfoInternalIds.CreateCustomer).
Data Source Configuration
The connector receives customer data as an XML document within the internal request's input stream. This XML is then transformed into App4Sales Customer objects for processing. The XML structure is expected to contain elements that map to the fields described in the 'Payload Mapping' section below.
Payload Mapping
The incoming XML payload is parsed into an App4Sales Customer object, which is then mapped to the PrestaShop customer and address entities. The following tables detail the mapping from the App4Sales internal data fields (derived from the incoming XML) to the PrestaShop API fields.
Core Customer Fields (App4Sales to PrestaShop)
App4Sales Field | Source Field (XML DataRow) | PrestaShop Target Field | Logic/Notes |
|
| N/A | Used internally in App4Sales. |
|
|
| If the incoming XML contains |
|
|
| Maps to the company name in PrestaShop. |
|
|
| Sets the customer's email address. |
|
|
| The full name is split into first name (first word) and last name (remaining words). |
N/A | N/A |
| Hardcoded to "8680b93fc1cd38c19fffa7686709956f". |
N/A | N/A |
| Hardcoded to |
N/A | N/A |
| Hardcoded to |
N/A | N/A |
| Set from the connector's |
Address Fields (App4Sales to PrestaShop)
The connector supports both a 'delivery' and 'visit' address. Addresses are created or updated if AddressLine1 is present. The VatCode from the customer is also applied to the address.
App4Sales Field | Source Field (XML DataRow) | PrestaShop Target Field | Logic/Notes |
|
|
| Delivery address line 1. |
|
|
| Delivery address line 2. |
|
|
| Delivery address postal code. |
|
|
| Delivery address city. |
|
|
| Visit address line 1. |
|
|
| Visit address line 2. |
|
|
| Visit address postal code. |
|
|
| Visit address city. |
|
|
| Uses the contact phone for both delivery and visit addresses. |
|
|
| Uses the contact email for both delivery and visit addresses. |
|
|
| Uses the contact fax for both delivery and visit addresses. |
|
|
| The ISO2 country code from the XML is used to look up the internal PrestaShop country ID via |
|
|
| The VAT code from the App4Sales customer is assigned to the PrestaShop address. |
N/A | N/A |
| Hardcoded to "App4Sales address " followed by a random 5-character hex number. |
Other Customer Fields (App4Sales to PrestaShop, not directly mapped to PrestaShop customer object)
App4Sales Field | Source Field (XML DataRow) | Logic/Notes |
|
| If present, stored in the App4Sales customer object. Used for address VAT number. |
|
| Parsed as a decimal. |
|
| Stored in the App4Sales customer object. |
|
| Parsed as an integer. |
|
| Looks up the payment condition code in the App4Sales database. If not found, uses the raw value from the XML. |
|
| Parsed as a boolean. |
|
| Stored as a string. |
Validation & Defaults
**Required Fields:** The connector expects at least
contactfullnameand an address withdeliveryAddress1orvisitAddress1to create a functional customer and address.**Default Values:**
New PrestaShop customers are always marked as
active = 1.id_shop_groupis hardcoded to1.A default, hardcoded password is assigned to new PrestaShop customers.
If
debCodeis not provided in the incoming XML, a new customer is created in PrestaShop.Addresses are only created if
AddressLine1is not empty.If the
countryCodeis not available in the incoming XML for an address, thelanguageCodeis used as a fallback for the ISO2 country code.Address aliases are generated with a random hex number.
**Data Transformations:**
The
contactfullnamefrom the XML is split to derivefirstnameandlastnamefor the PrestaShop customer.ISO2 country codes are converted to full country names for internal App4Sales address objects.
discountis parsed to a decimal,usesPriceto an integer, andisVatLiableto a boolean.
Response & Error Handling
**Duplicate Detection:** The connector attempts to find an existing PrestaShop customer using the
CustomerCode(which maps todebCodein the incoming XML). If a customer with this ID exists, their record is updated; otherwise, a new customer is created.**PrestaShop API Responses:** The connector interacts with the PrestaShop API using
CustomerFactoryandAddressFactoryto add or update customer and address entities.**Error Handling:** The current implementation uses basic try-catch blocks. In case of an exception during API calls, it might result in a partial update or failure to create a customer/address without specific error messages returned to the App4Sales UI beyond generic exceptions.
**New Codes Storage:** Upon successful creation or update of a PrestaShop customer, their PrestaShop ID is appended to the return message. Additionally, the PrestaShop address IDs are stored in the
KeySettingsContextusing keys like "CustomerAddress_" +prestaCustomer.idand "CustomerAddressLink_" +prestaCustomer.id. This allows the connector to link App4Sales customer IDs to their corresponding PrestaShop IDs for future operations.
Special Logic & Filters
**Address Update/Creation Logic:** When processing addresses, the connector first checks if an address with the same
AddressLine1already exists for the customer in PrestaShop. If a match is found, the existing address is updated; otherwise, a new address is created.**Random Address Alias:** New addresses in PrestaShop are assigned an alias in the format "App4Sales address [random_hex_number]".
Related Settings & Prerequisites
The following connector settings influence the customer creation process:
**Id Shop:** This setting specifies the PrestaShop shop ID to which new customers will be associated.
**Application Key:** This is the API key used to authenticate requests to the PrestaShop API.
**Customer Code For Field:** If configured, this setting determines which field in the PrestaShop customer object maps to the App4Sales
CustomerCodefor identifying existing customers. If not set, the PrestaShop customer ID is used directly as theCustomerCode.
Known Limitations
The password for new PrestaShop customers is hardcoded, which might be a security concern or require manual updates in PrestaShop after creation.
The country code mapping relies on
KeySettingsContext. If the mapping is incorrect or missing, the address country might not be correctly set in PrestaShop.Error handling is general; specific PrestaShop validation errors are not explicitly parsed and returned to the App4Sales platform.
The code contains a TODO comment regarding country code handling, indicating a known area for improvement: "TODO find another way to handle these Country codes -- when we start using the PrestaShop connector again :-)".