cancel
Showing results for 
Search instead for 
Did you mean: 

Email validation customer setConfirmation

SOLVED

Email validation customer setConfirmation

Hello,

I would like a user does not have to validate his account (only specific user from custom register form).

 

I use this:

$customer->setConfirmation(null);

$customer->save();

 

that works fine, but the confirmation email still sent...

How to not sent the email if I use setConfirmation(null)?

 

any idea?

thank Smiley Happy

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Email validation customer setConfirmation

Hi @JoouulDev

 

Sorry for the dealy Man Happy

 

which event you are using?


I have checked with this event

<event name="customer_save_before">
        <observer name="my_customer_save_before" instance="\Mymodule\Module\Observer\BehaviorCustomerBeforeSave"/>
    </event>

and only put this code in observer,  not need to save customer in observer

 

 $customer = $observer->getEvent()->getCustomer();

$customer->setConfirmation(null); if ($this->registry->registry('skip_confirmation_if_email')) { $this->registry->unregister('skip_confirmation_if_email'); } $this->registry->register('skip_confirmation_if_email', $customer->getEmail());

 and it is working

 

Please check with this.

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Thanks Smiley Happy

View solution in original post

7 REPLIES 7

Re: Email validation customer setConfirmation

 

Hi @JoouulDev

 

Please use this code before customer save


if ($this->registry->registry('skip_confirmation_if_email')) { $this->registry->unregister('skip_confirmation_if_email'); } $this->registry->register('skip_confirmation_if_email', $customerEmailAddress);

$customerEmailAddress=> specific user email which for you want to skip confirmation

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Thanks Smiley Happy

Re: Email validation customer setConfirmation

Hi Geeta,

 

thank for your reply.

 

I do this before $customer->save(); and I called this -> use Magento\Framework\Registry; because it's a script wich implements ObserverInterface

 

$customer->setConfirmation(null);

if ($this->registry->registry('skip_confirmation_if_email')) {
    $this->registry->unregister('skip_confirmation_if_email');
}

$this->registry->register('skip_confirmation_if_email', $customerEmailAddress);

but email still sended.

 

 

 

Re: Email validation customer setConfirmation

Hi @JoouulDev

Can you please send complete class code. I will check and then reply to you with a solution

If you find my answer useful, Please click Kudos & Accept as Solution.

Thanks Smiley Happy

Re: Email validation customer setConfirmation

Hi,

this is my code

 

<?php

namespace MyModule\ModuleName\Actions;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;

class CustomClass implements ObserverInterface
{
    protected $_responseFactory;
    protected $_request;
    protected $_url;
    private $registry;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\UrlInterface $url,
        Registry $registry
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_request = $request;
        $this->_url = $url;
        $this->registry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        $post = $this->_request->getPost();

        // $_POST from 2 form. First form 
        // type-registration == 1 (user have to confirm account by email, ok in back-office)
        // type-registration == 2 (user do not confirmed account and email not send)
        if ($post['type-registration'] == 2) {
            $customer->setConfirmation(null);

            if ($this->registry->registry('skip_confirmation_if_email')) {
                $this->registry->unregister('skip_confirmation_if_email');
            }

            $this->registry->register('skip_confirmation_if_email', $customer->getEmail());
            $returnUrl = 'custom_url_success';
        } else {
            $returnUrl = 'custom_url_error';
        }

        try {
            $customer = $customerRepository->save($customer);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        $this->_responseFactory->create()->setRedirect($this->_url->getUrl($returnUrl))->sendResponse();
        exit();
    }
}

Thank for help Smiley Happy

Re: Email validation customer setConfirmation

@Geeta Modi any solution ? Smiley Happy

Re: Email validation customer setConfirmation

Hi @JoouulDev

 

Sorry for the dealy Man Happy

 

which event you are using?


I have checked with this event

<event name="customer_save_before">
        <observer name="my_customer_save_before" instance="\Mymodule\Module\Observer\BehaviorCustomerBeforeSave"/>
    </event>

and only put this code in observer,  not need to save customer in observer

 

 $customer = $observer->getEvent()->getCustomer();

$customer->setConfirmation(null); if ($this->registry->registry('skip_confirmation_if_email')) { $this->registry->unregister('skip_confirmation_if_email'); } $this->registry->register('skip_confirmation_if_email', $customer->getEmail());

 and it is working

 

Please check with this.

 

If you find my answer useful, Please click Kudos & Accept as Solution.

Thanks Smiley Happy

Re: Email validation customer setConfirmation

@Geeta Modi GREAT Smiley Happy

I'used this event "customer_register_success" and with "customer_save_before"

Thank you very much Smiley Happy