Jump to content

[SOLVED] Custom SQL right after account created [prestashop 1.7]


Recommended Posts

Hi, 

I'm currently creating some custom modules.

I need to do some sql select, update and insert right after a customer created his account.

To do my sql, I need the e-mail used and the ID of this new customer.

I don't know where to do that properly.

Thank's in advance

Edited by batou (see edit history)
Link to comment
Share on other sites

Not possible to add smth like override script? 

I have a table with email invited and I want to compare new customer with this table. If they match, do 2 insert sql with the new id_customer

If not, I will create new module :) thank you for your help

Link to comment
Share on other sites

Thank you Nishith.

Can I add my sql request directly in this hook ?

Something like ?

    public function hookActionObjectCustomerAddAfter($params)
    {
       $id_customer = $params['newCustomer']->id; //get your customer id here
       $customerEmail = $params['newCustomer']->email; //get your customer email here
       $sql = '
     		SELECT `id_list` FROM `ilea_join_list_new_customer` WHERE email='.$customerEmail;
        if ($results = Db::getInstance()->ExecuteS($sql)){
          foreach ($results as $curent_id_list) {
            Db::getInstance()->insert('ilea_join_list_customer', array(
              'id_customer' => (int)$id_customer,
              'id_giftlist' => (int)$curent_id_list
            ), false, true, Db::INSERT, false);
          }
        }
    }

> didn't work even with your hook hookActionCustomerAccountAdd  :(

Edited by batou (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

It didn't work because your code is not right...

    /**
     * Install Module.
     *
     * @return bool
     */
    public function install()
    {
        return parent::install()
            && $this->registerHook('actionCustomerAccountAdd');
    }

    /**
     * Retrieve Customer data after account creation.
     *
     * @param array $params
     */
    public function hookActionCustomerAccountAdd(array $params)
    {
        if (isset($params['newCustomer'])
            && Validate::isLoadedObject($params['newCustomer'])
        ) {
            $giftList = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
                SELECT `id_list`
                FROM `ilea_join_list_new_customer`
                WHERE `email` LIKE "'.pSQL($params['newCustomer']->email).'"
            ');

            if ($giftList) {
                foreach ($giftList as $giftListItem) {
                    Db::getInstance()->execute('
                        INSERT INTO `ilea_join_list_customer` (`id_customer`, `id_giftlist`)
                        VALUES (' . (int) $params['newCustomer']->id . ', ' . (int) $giftListItem['id_list'] . ')
                    ');
                }
            }
        }
    }

If your module is already installed, reset it for hook registration.

Edited by Janett (see edit history)
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...