Jump to content

Customer Update Email Code


TinyStore

Recommended Posts

Hi,

Does anyone know where the customer update email code is located?  I'm looking for the code that updates the database when a customer updates their email.

What I'm trying to do is override this code to add an entry in the database to keep the old email as a deleted account for now and probably add a note that the email was changed by the customer.  I don't want customers to change their email so they can continuously register with the same email(s).

I don't see it in the customer class update function.

I found the validation in the IdentityController but I don't want to make this change during the validation.

Thanks.

Link to comment
Share on other sites

I hacked into the PS code for now to accomplish what I'm trying to do but want to add this as a hook eventually.

 

In Customer.php you can add in the update function:

        $customer = $this->getCustomer($this->id);  
        $oldemail = $customer[0]['email'];

        if ($oldemail != $this->email) {
        Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'customer` (`firstname`, `lastname`, `email`, `note`, `active`, `deleted`)  VALUES ("'.$this->firstname.'", "'.$this->lastname.'", "'.$oldemail.'", "Old email", 0, 1)');
        }

 

You also need to add this function to Customer.php for it to work:

        /**
     * Return customer.
     *
     *
     * @return array Customer
     */
    public static function getCustomer($idCustomer)
    {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT `id_customer`, `email`, `firstname`, `lastname`
            FROM `'._DB_PREFIX_.'customer`
            WHERE `id_customer` = \''.(int) $idCustomer.'\'');
    }

 

Link to comment
Share on other sites

This is the final draft of this mod.

Create override/classes/Customer.php with the code below or add it.

 

<?php
class Customer extends CustomerCore
{
               
      /**
     * Override customer update to record old emails.
     *
     */
    public function update($nullValues = false)  {      
        $customer = $this->getCustomer($this->id);  
        $oldemail = $customer[0]['email'];

        if ($oldemail != $this->email) {
        Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'customer` (`firstname`, `lastname`, `email`, `note`, `active`, `deleted`)  VALUES ("'.$this->firstname.'", "'.$this->lastname.'", "'.$oldemail.'", "Old email", 0, 1)');
        }
               $this->birthday = (empty($this->years) ? $this->birthday : (int) $this->years.'-'.(int) $this->months.'-'.(int) $this->days);

        if ($this->newsletter && !Validate::isDate($this->newsletter_date_add)) {
            $this->newsletter_date_add = date('Y-m-d H:i:s');
        }
        if (isset(Context::getContext()->controller) && Context::getContext()->controller->controller_type == 'admin') {
            $this->updateGroup($this->groupBox);
        }

        if ($this->deleted) {
            $addresses = $this->getAddresses((int) Configuration::get('PS_LANG_DEFAULT'));
            foreach ($addresses as $address) {
                $obj = new Address((int) $address['id_address']);
                $obj->delete();
            }
        }

        try {
            return parent::update(true);
        } catch (\PrestaShopException $exception) {
            $message = $exception->getMessage();
            error_log($message);

            return false;
        }
    }    
    
            /**
     * Return customer.
     *
     *
     * @return array Customer
     */
    public static function getCustomer($idCustomer)
    {
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
            SELECT `id_customer`, `email`, `firstname`, `lastname`
            FROM `'._DB_PREFIX_.'customer`
            WHERE `id_customer` = \''.(int) $idCustomer.'\'');
    }
}

 

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