1

Here is my code

customerregistersuceessobserver.php

    <?php


namespace Webkul\Marketplace\Observer\Rewrite;
use Webkul\Marketplace\Observer\CustomerRegisterSuccessObserver;
use Magento\Framework\Event\ObserverInterface;
use Webkul\Marketplace\Model\ResourceModel\Seller\CollectionFactory;


class CustomerRewriteRegisterSuccessObserver extends CustomerRegisterSuccessObserver
{
    /**
     * @var ObjectManagerInterface
     */
    protected $_objectManager;

    /**
     * @var CollectionFactory
     */
    protected $_collectionFactory;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_date;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    private $_messageManager;

    /**
     * @param \Magento\Framework\ObjectManagerInterface   $objectManager
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
     * @param \Magento\Store\Model\StoreManagerInterface  $storeManager
     * @param \Magento\Framework\Message\ManagerInterface $messageManager,
     * @param CollectionFactory                           $collectionFactory
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        CollectionFactory $collectionFactory
    ) {
        $this->_objectManager = $objectManager;
        $this->_storeManager = $storeManager;
        $this->_messageManager = $messageManager;
        $this->_collectionFactory = $collectionFactory;
        $this->_date = $date;
    }

    /**
     * customer register event handler.
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $data = $observer['account_controller'];
        try {
            $paramData = $data->getRequest()->getParams();
            if (!empty($paramData['is_seller']) && !empty($paramData['selling_categories']) && $paramData['is_seller'] == 1) {
                $customer = $observer->getCustomer();

                //~ $profileurlcount = $this->_objectManager->create(
                    //~ 'Webkul\Marketplace\Model\Seller'
                //~ )->getCollection();
                //~ $profileurlcount->addFieldToFilter(
                    //~ 'selling_categories',
                    //~ $paramData['selling_categories']
                //~ );
                //~ if (!$profileurlcount->getSize()) {
                    $status = $this->_objectManager->get(
                        'Webkul\Marketplace\Helper\Data'
                    )->getIsPartnerApproval() ? 0 : 1;
                    $customerid = $customer->getId();
                    $model = $this->_objectManager->create(
                        'Webkul\Marketplace\Model\Seller'
                    );
                    $categories=implode(',',$paramData['selling_categories']);
                    $model->setData('is_seller', $status);
                    $model->setData('selling_categories',$categories);
                    $model->setData('seller_id', $customerid);
                    $model->setData('store_id', 0);
                    $model->setCreatedAt($this->_date->gmtDate());
                    $model->setUpdatedAt($this->_date->gmtDate());
                    if ($status == 0) {
                        $model->setAdminNotification(1);
                    }
                    $model->save();
                    $loginUrl = $this->_objectManager->create(
                        'Magento\Framework\UrlInterface'
                    )->getUrl("marketplace/account/dashboard");
                    $this->_objectManager->create(
                        'Magento\Customer\Model\Session'
                    )->setBeforeAuthUrl($loginUrl);
                    $this->_objectManager->create(
                        'Magento\Customer\Model\Session'
                    )->setAfterAuthUrl($loginUrl);

                    $helper = $this->_objectManager->get(
                        'Webkul\Marketplace\Helper\Data'
                    );
                    //~ if ($helper->getAutomaticUrlRewrite()) {
                        //~ $this->createSellerPublicUrls($paramData['profileurl']);
                    //~ }
                    $adminStoremail = $helper->getAdminEmailId();
                    $adminEmail = $adminStoremail ? $adminStoremail : $helper->getDefaultTransEmailId();
                    $adminUsername = 'Admin';
                    $senderInfo = [
                        'name' => $customer->getFirstName().' '.$customer->getLastName(),
                        'email' => $customer->getEmail(),
                    ];
                    $receiverInfo = [
                        'name' => $adminUsername,
                        'email' => $adminEmail,
                    ];
                    $emailTemplateVariables['myvar1'] = $customer->getFirstName().' '.
                    $customer->getLastName();
                    $emailTemplateVariables['myvar2'] = $this->_objectManager->get(
                        'Magento\Backend\Model\Url'
                    )->getUrl(
                        'customer/index/edit',
                        ['id' => $customer->getId()]
                    );
                    $emailTemplateVariables['myvar3'] = 'Admin';

                    $this->_objectManager->create(
                        'Webkul\Marketplace\Helper\Email'
                    )->sendNewSellerRequest(
                        $emailTemplateVariables,
                        $senderInfo,
                        $receiverInfo
                    );







                //~ } else {
                    //~ $this->_messageManager->addError(
                        //~ __('This Shop URL already Exists.')
                    //~ );
                //~ }
            }
        } catch (\Exception $e) {
            $this->_messageManager->addError($e->getMessage());
        }
    }

here I need to redirect the customer after register in magento 2 how i can set redirect (note: email verfication enabled)

how i can do this

now it redirects to the customer login page

2

1 Answer 1

0

Using Plugin :-

di.xml :

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Customer\Model\Account\Redirect">
        <plugin name="NewAcctRedirectPlug" type="Your\Module\Plugin\Redirect" disabled="false" sortOrder="1"/>
    </type>

</config>

events.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="custom_redirect_register_success" instance="Your\Module\Observer\Register" disabled="false"/>
    </event>
</config>

Register.php:

<?php

namespace Your\Module\Observer;


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

class Register implements ObserverInterface
{
    protected $coreRegistry;

    public function __construct(Registry $registry)
    {
        $this->coreRegistry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->coreRegistry->register('is_new_account', true);
    }
}

Redirect.php:

<?php

namespace Your\Module\Plugin;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;

class Redirect
{
    protected $coreRegistry;

    protected $url;

    protected $resultFactory;

    public function __construct(Registry $registry, UrlInterface $url, ResultFactory $resultFactory)
    {
        $this->coreRegistry = $registry;
        $this->url = $url;
        $this->resultFactory = $resultFactory;
    }

    public function aroundGetRedirect ($subject, \Closure $proceed)
    {
        if ($this->coreRegistry->registry('is_new_account')) {
            /** @var \Magento\Framework\Controller\Result\Redirect $result */
            $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $result->setUrl($this->url->getUrl('custom/path'));
            return $result;
        }

        return $proceed();
    }
}
1
  • what is return $proceed(); for some reason after register i am getting return back to login page Commented Apr 24, 2020 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.