How To Create New Customer Address Programatically In Prestashop

This tutorial will show you the required steps you need to implement in order to create new customer address programatically in prestashop. First step we will need to create a postProcess function in your module. If you don't have already a module created you can override the FrontController.php and add it the that file. So open up override folder and use the prestashop default folders structure to create a new file and name it: FrontController.php

Add function to create new customer address

Now, we have to create the postProcess function which will be triggered and we need.

The function will look like the following:

  $fields = $_POST;

We get all parameters which are in the $_POST request and send them our function which looks like below:

public function insertNewAddress($address){
    $AddressObject = new Address();
        $AddressObject->id_customer = $this->context->customer->id; 
        $AddressObject->firstname = pSQL($address->firstname);
        $AddressObject->lastname = pSQL($address->lastname);
        $AddressObject->address1 = pSQL($address->address1);
    $AddressObject->company = pSQL($address->company);
    $AddressObject->vat_number = pSQL($address->vat_number);
    $AddressObject->company_registration_number = pSQL($address->company_registration_number);
    $AddressObject->postcode = pSQL($address->postcode);
        $AddressObject->city = pSQL($address->city);
    $AddressObject->id_country = (int)$address->country; 
        $AddressObject->alias = pSQL($address->address_name);
    return $AddressObject->id;
    $this->context->cookie->__set('custom_address_id', $AddressObject->id);

And that's it , you can use the above code to add a new customer address programatically, I hope it will help you.

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

There is an error. Should be $this->createNewAddress($fields); —-> $this-> insertNewAddress($fields);

Would love your thoughts, please comment.x