Jump to content

Getting an account verified by admin


Pedro Horta

Recommended Posts

Greetings,

i'm fairly new to prestashop, and I've been trying to implement a feature to the website I'm currently working on, and I'm stuck with a few issues I can't seem to overcome
When a new customer registers in the website, he is "by default" disabled, so the admin can verify his account, and giving him acess to his account, so he could start buying from the store.
I've managed to do the disable/enable part of it, but I'm strugling on sending an e-mail notifying the user he's now able to shop.
I've tried creating the templates, as well as the code I'll leave bellow
 

///home/store/public_html/override/classes/Customer.php

class Customer extends CustomerCore
{
    /**
     * Set the active status of the customer and send an activation email if activated.
     *
     * @param bool $status The new active status of the customer
     * @return bool Whether the update was successful
     */
    public function setActive($status)
    {
        $this->active = (bool)$status;
        $result = $this->update();

        if ($result && $status) {
            // If the customer is being activated, send the activation email
            $this->sendActivationEmail();
        }

        return $result;
    }

    /**
     * Send the account activation email to the customer.
     */
    private function sendActivationEmail()
    {
        // Prepare email data
        $template = 'account_activated';  // Email template name (without extension)
        $subject = 'A sua conta foi ativada';  // Subject of the email
        $to = $this->email;  // Customer's email
        $toName = $this->firstname . ' ' . $this->lastname;  // Customer's full name

        // Prepare email variables
        $data = array(
            '{firstname}' => $this->firstname,
            '{lastname}' => $this->lastname,
            '{shop_name}' => Configuration::get('PS_SHOP_NAME')  // Shop name from the configuration
        );

        // Log for debugging purposes
        PrestaShopLogger::addLog('Sending activation email to: ' . $to, 1);

        // Send the email using the Mail::Send method
        $result = Mail::Send(
            (int)$this->id_lang,        // Customer's language ID
            $template,                  // Template name
            $subject,                   // Subject of the email
            $data,                      // Data to replace in the template
            $to,                        // Recipient's email
            $toName,                    // Recipient's name
            null,                       // From email (null to use default)
            null,                       // From name (null to use default)
            null,                       // File attachment
            null,                       // Mode (not required)
            _PS_MAIL_DIR_,              // Mail directory (default for templates)
            false,                      // Don't use SMTP debugging
            (int)$this->id_shop         // Shop ID
        );

        // Log the result of the email sending
        if (!$result) {
            PrestaShopLogger::addLog('Error sending activation email to: ' . $to, 3);
        } else {
            PrestaShopLogger::addLog('Activation email sent successfully to: ' . $to, 1);
        }
    }
}

What am I missing? Is this the optimal way to do so? Could I , perhaps, do it with guest mode?

 Thanks in advance

Link to comment
Share on other sites

Hi, you should use a hook hookActionObjectCustomerUpdateAfter, something like:
 

  public function hookActionObjectCustomerUpdateAfter(array $params) {
    if($this->context->controller->controller_name == 'Admin') {
      $customer               = new Customer($params['object']->id);
      $customer_status_after  = $params['object']->active;

      if ($this->customer_status_before != $customer_status_after && $customer_status_after == true) {
            $mailtpl = 'account_enable';
            $subject = $this->l('Account enabled', false, Language::getLocaleById($customer->id_lang));
            Mail::send(...
      }
    }
  }

 

Link to comment
Share on other sites

Hi,
Instead of overriding the Customer class, you might want to use PrestaShop's hooks, which is a more modular approach.

The hook actionObjectCustomerUpdateAfter could be used to trigger an email when a customer’s account is activated, rather than modifying the Customer class directly.

public function hookActionObjectCustomerUpdateAfter($params)
{
$customer = $params['object'];
if ($customer->active) {
// Call your sendActivationEmail logic here
}
}

Regards.

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