Home is for the experience. Projects are for the ones who are interested!

How To Create New Customer Address Programatically In Prestashop

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:

if(Tools::isSubmit('create_new_address')){
  $fields = $_POST;
  $this->createNewAddress($fields);
}

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);
        $AddressObject->add();
    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.

Leave a Reply

Your email address will not be published. Required fields are marked *