Jump to content

Edit History

c64girl

c64girl


Fixed code

Edit 2:

 

Ok i found the way, in ps 1.7.8.11

edit: src/Adapter/Customer/CommandHandler/AddCustomerHandler.php

 

Added to code

On top:

use Mail;

In middle:

        // Send welcome email to customer
        $this->sendCustomerWelcomeEmail($customer, $command->getPassword()->getValue());

At end bfore last }

    /**
     * Sends a welcome email to the newly added customer
     *
     * @param Customer $customer
     */
    private function sendCustomerWelcomeEmail(Customer $customer, $plainPassword)
    {
        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
        ];

        $languageId = $customer->id_lang ? (int) $customer->id_lang : 1;

        $mailSent = Mail::Send(
            $languageId,
            'account',
            Mail::l('Welcome!', $languageId),
            $templateVars,
            $customer->email,
            $customer->firstname . ' ' . $customer->lastname
        );

        if (!$mailSent) {
            // Handle the case where the email could not be sent
            throw new \RuntimeException('Failed to send welcome email to customer');
        }
    }

 

Edited code look like this:

namespace PrestaShop\PrestaShop\Adapter\Customer\CommandHandler;

use Customer;
use Mail;
use PrestaShop\PrestaShop\Core\Crypto\Hashing;
use PrestaShop\PrestaShop\Core\Domain\Customer\Command\AddCustomerCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\CommandHandler\AddCustomerHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerDefaultGroupAccessException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\DuplicateCustomerEmailException;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\CustomerId;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\RequiredField;
use PrestaShop\PrestaShop\Core\Domain\ValueObject\Email;

/**
 * Handles command that adds new customer
 *
 * @internal
 */
final class AddCustomerHandler extends AbstractCustomerHandler implements AddCustomerHandlerInterface
{
    /**
     * @var Hashing
     */
    private $hashing;

    /**
     * @var string Value of legacy _COOKIE_KEY_
     */
    private $legacyCookieKey;

    /**
     * @param Hashing $hashing
     * @param string $legacyCookieKey
     */
    public function __construct(Hashing $hashing, $legacyCookieKey)
    {
        $this->hashing = $hashing;
        $this->legacyCookieKey = $legacyCookieKey;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(AddCustomerCommand $command)
    {
        $customer = new Customer();

        $this->fillCustomerWithCommandData($customer, $command);

        // validateFieldsRequiredDatabase() below is using $_POST
        // to check if required fields are set
        $_POST[RequiredField::PARTNER_OFFERS] = $command->isPartnerOffersSubscribed();

        $this->assertRequiredFieldsAreNotMissing($customer);

        if (false === $customer->validateFields(false)) {
            throw new CustomerException('Customer contains invalid field values');
        }

        $this->assertCustomerWithGivenEmailDoesNotExist($command->getEmail());
        $this->assertCustomerCanAccessDefaultGroup($command);

        $customer->add();
		
		// Send welcome email to customer
        $this->sendCustomerWelcomeEmail($customer, $command->getPassword()->getValue());

        return new CustomerId((int) $customer->id);
    }

    /**
     * @param Email $email
     */
    private function assertCustomerWithGivenEmailDoesNotExist(Email $email)
    {
        $customer = new Customer();
        $customer->getByEmail($email->getValue());

        if ($customer->id) {
            throw new DuplicateCustomerEmailException($email, sprintf('Customer with email "%s" already exists', $email->getValue()));
        }
    }

    /**
     * @param Customer $customer
     * @param AddCustomerCommand $command
     */
    private function fillCustomerWithCommandData(Customer $customer, AddCustomerCommand $command)
    {
        $apeCode = null !== $command->getApeCode() ?
            $command->getApeCode()->getValue() :
            null;

        $hashedPassword = $this->hashing->hash(
            $command->getPassword()->getValue(),
            $this->legacyCookieKey
        );

        $customer->firstname = $command->getFirstName()->getValue();
        $customer->lastname = $command->getLastName()->getValue();
        $customer->email = $command->getEmail()->getValue();
        $customer->passwd = $hashedPassword;
        $customer->id_default_group = $command->getDefaultGroupId();
        $customer->groupBox = $command->getGroupIds();
        $customer->id_gender = $command->getGenderId();
        $customer->active = $command->isEnabled();
        $customer->optin = $command->isPartnerOffersSubscribed();
        $customer->birthday = $command->getBirthday()->getValue();
        $customer->id_shop = $command->getShopId();

        // fill b2b customer fields
        $customer->company = $command->getCompanyName();
        $customer->siret = $command->getSiretCode();
        $customer->ape = $apeCode;
        $customer->website = $command->getWebsite();
        $customer->outstanding_allow_amount = $command->getAllowedOutstandingAmount();
        $customer->max_payment_days = $command->getMaxPaymentDays();
        $customer->id_risk = $command->getRiskId();
    }

    /**
     * @param AddCustomerCommand $command
     */
    private function assertCustomerCanAccessDefaultGroup(AddCustomerCommand $command)
    {
        if (!in_array($command->getDefaultGroupId(), $command->getGroupIds())) {
            throw new CustomerDefaultGroupAccessException(sprintf('Customer default group with id "%s" must be in access groups', $command->getDefaultGroupId()));
        }
    }
    /**
     * Sends a welcome email to the newly added customer
     *
     * @param Customer $customer
     */
    private function sendCustomerWelcomeEmail(Customer $customer, $plainPassword)
    {
        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
        ];

        $languageId = $customer->id_lang ? (int) $customer->id_lang : 1;

        $mailSent = Mail::Send(
            $languageId,
            'account',
            Mail::l('Welcome!', $languageId),
            $templateVars,
            $customer->email,
            $customer->firstname . ' ' . $customer->lastname
        );

        if (!$mailSent) {
            // Handle the case where the email could not be sent
            throw new \RuntimeException('Failed to send welcome email to customer');
        }
    }
}

 

 

c64girl

c64girl

Edit 2:

 

Ok i found the way, in ps 1.7.8.11

edit: src/Adapter/Customer/CommandHandler/AddCustomerHandler.php

 

Added to code

On top:


use Mail;

In middle:

        // Send welcome email to customer
        $this->sendCustomerWelcomeEmail($customer, $command->getPassword()->getValue());

At end bfore last }

    /**
     * Sends a welcome email to the newly added customer
     *
     * @param Customer $customer
     * @param string $plainPassword
     */
    private function sendCustomerWelcomeEmail(Customer $customer, $plainPassword)
    {
        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
            '{passwd}' => $plainPassword,
        ];

        $languageId = $customer->id_lang ? (int) $customer->id_lang : 1;

        $mailSent = Mail::Send(
            $languageId,
            'account',
            Mail::l('Welcome!', $languageId),
            $templateVars,
            $customer->email,
            $customer->firstname . ' ' . $customer->lastname
        );

        if (!$mailSent) {
            // Handle the case where the email could not be sent
            throw new \RuntimeException('Failed to send welcome email to customer');
        }
    }

 

Edited code look like this:

namespace PrestaShop\PrestaShop\Adapter\Customer\CommandHandler;

use Customer;
use Mail;
use PrestaShop\PrestaShop\Core\Crypto\Hashing;
use PrestaShop\PrestaShop\Core\Domain\Customer\Command\AddCustomerCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\CommandHandler\AddCustomerHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerDefaultGroupAccessException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\CustomerException;
use PrestaShop\PrestaShop\Core\Domain\Customer\Exception\DuplicateCustomerEmailException;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\CustomerId;
use PrestaShop\PrestaShop\Core\Domain\Customer\ValueObject\RequiredField;
use PrestaShop\PrestaShop\Core\Domain\ValueObject\Email;

/**
 * Handles command that adds new customer
 *
 * @internal
 */
final class AddCustomerHandler extends AbstractCustomerHandler implements AddCustomerHandlerInterface
{
    /**
     * @var Hashing
     */
    private $hashing;

    /**
     * @var string Value of legacy _COOKIE_KEY_
     */
    private $legacyCookieKey;

    /**
     * @param Hashing $hashing
     * @param string $legacyCookieKey
     */
    public function __construct(Hashing $hashing, $legacyCookieKey)
    {
        $this->hashing = $hashing;
        $this->legacyCookieKey = $legacyCookieKey;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(AddCustomerCommand $command)
    {
        $customer = new Customer();

        $this->fillCustomerWithCommandData($customer, $command);

        // validateFieldsRequiredDatabase() below is using $_POST
        // to check if required fields are set
        $_POST[RequiredField::PARTNER_OFFERS] = $command->isPartnerOffersSubscribed();

        $this->assertRequiredFieldsAreNotMissing($customer);

        if (false === $customer->validateFields(false)) {
            throw new CustomerException('Customer contains invalid field values');
        }

        $this->assertCustomerWithGivenEmailDoesNotExist($command->getEmail());
        $this->assertCustomerCanAccessDefaultGroup($command);

        $customer->add();
		
		// Send welcome email to customer
        $this->sendCustomerWelcomeEmail($customer, $command->getPassword()->getValue());

        return new CustomerId((int) $customer->id);
    }

    /**
     * @param Email $email
     */
    private function assertCustomerWithGivenEmailDoesNotExist(Email $email)
    {
        $customer = new Customer();
        $customer->getByEmail($email->getValue());

        if ($customer->id) {
            throw new DuplicateCustomerEmailException($email, sprintf('Customer with email "%s" already exists', $email->getValue()));
        }
    }

    /**
     * @param Customer $customer
     * @param AddCustomerCommand $command
     */
    private function fillCustomerWithCommandData(Customer $customer, AddCustomerCommand $command)
    {
        $apeCode = null !== $command->getApeCode() ?
            $command->getApeCode()->getValue() :
            null;

        $hashedPassword = $this->hashing->hash(
            $command->getPassword()->getValue(),
            $this->legacyCookieKey
        );

        $customer->firstname = $command->getFirstName()->getValue();
        $customer->lastname = $command->getLastName()->getValue();
        $customer->email = $command->getEmail()->getValue();
        $customer->passwd = $hashedPassword;
        $customer->id_default_group = $command->getDefaultGroupId();
        $customer->groupBox = $command->getGroupIds();
        $customer->id_gender = $command->getGenderId();
        $customer->active = $command->isEnabled();
        $customer->optin = $command->isPartnerOffersSubscribed();
        $customer->birthday = $command->getBirthday()->getValue();
        $customer->id_shop = $command->getShopId();

        // fill b2b customer fields
        $customer->company = $command->getCompanyName();
        $customer->siret = $command->getSiretCode();
        $customer->ape = $apeCode;
        $customer->website = $command->getWebsite();
        $customer->outstanding_allow_amount = $command->getAllowedOutstandingAmount();
        $customer->max_payment_days = $command->getMaxPaymentDays();
        $customer->id_risk = $command->getRiskId();
    }

    /**
     * @param AddCustomerCommand $command
     */
    private function assertCustomerCanAccessDefaultGroup(AddCustomerCommand $command)
    {
        if (!in_array($command->getDefaultGroupId(), $command->getGroupIds())) {
            throw new CustomerDefaultGroupAccessException(sprintf('Customer default group with id "%s" must be in access groups', $command->getDefaultGroupId()));
        }
    }
    /**
     * Sends a welcome email to the newly added customer
     *
     * @param Customer $customer
     * @param string $plainPassword
     */
    private function sendCustomerWelcomeEmail(Customer $customer, $plainPassword)
    {
        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
            '{passwd}' => $plainPassword,
        ];

        $languageId = $customer->id_lang ? (int) $customer->id_lang : 1;

        $mailSent = Mail::Send(
            $languageId,
            'account',
            Mail::l('Welcome!', $languageId),
            $templateVars,
            $customer->email,
            $customer->firstname . ' ' . $customer->lastname
        );

        if (!$mailSent) {
            // Handle the case where the email could not be sent
            throw new \RuntimeException('Failed to send welcome email to customer');
        }
    }
}

 

 

c64girl

c64girl

Edit:

 

I added:

    /**
     * Sends an email to the newly added customer
     *
     * @param Customer $customer
     */
    private function sendCustomerEmail(Customer $customer)
    {
        $templateVars = [
            '{firstname}' => $customer->firstname,
            '{lastname}' => $customer->lastname,
            '{email}' => $customer->email,
            // Add other variables if needed
        ];

        $mailSent = Mail::Send(
            (int) $customer->id_lang, // Language ID (you might need to set this appropriately)
            'new_customer', // Template name (make sure it exists in the /mails/ directory)
            'Welcome to our store', // Subject of the email
            $templateVars,
            $customer->email,
            $customer->firstname . ' ' . $customer->lastname
        );

        if (!$mailSent) {
            // Handle the case where the email could not be sent
            throw new \RuntimeException('Failed to send email to customer');
        }
    }	

 

×
×
  • Create New...