3

I have created custom module for custom blog wishlist. So I have set authentication while trying to save blog into wishlist as below Controller >> save.php

public function execute()
{
    $data = [];
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

    if(!$this->_customerSession->isLoggedIn()) {
        $resultRedirect->setPath($this->url->getCurrentUrl());
        $this->_customerSession->setAfterAuthUrl($this->url->getCurrentUrl());
        $this->_customerSession->authenticate();
        return false;
    }

    $requestParams = $this->getRequest()->getParams();

    $postId = $requestParams['postid'];
    if (!$postId) {
        $resultRedirect->setPath('*/');
        return $resultRedirect;
    }

    /** @var \VendorName\Blog\Model\Post\Wishlist $model */
    $model = $this->wishlistFactory->create();

    $data['post_id'] = $postId;
    $data['customer_id'] = $this->_customerSession->getCustomer()->getId();
    // $data['update_at'] = $date;

    $model->setData($data);

    try {
            $model->save();
            $this->messageManager->addSuccess(__('Post Saved.'));
            $this->dataPersistor->clear('blog_post');
            if ($this->getRequest()->getParam('back')) {
                return $resultRedirect->setPath('ideas-blog/category/home');
            }
            return $resultRedirect->setPath('blog/post/wishlist');
        } catch (LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            echo"<pre/>"; print_r($e->getMessage());exit;
            $this->messageManager->addException($e, __('Something went wrong while saving the post in wishlist.'));
        }

        $this->dataPersistor->set('blog_post', $data);
        return $resultRedirect->setPath('blog/post/wishlist');
}

$this->url->getCurrentUrl() has the current URL on which customer should be redirect after login successfully.

But I have created one plugin for another module which allow me to set redirect path after login :

public function afterExecute(
    \Magento\Customer\Controller\Account\LoginPost $subject,
    $result
    )
{
    $errorMessages = $this->messageManager->getMessages()->getErrors();
    if(!count($errorMessages))
        $result->setPath('customerview/account');

    return $result;
}

Above plugin will redirect to my custom account view page after normal login, but I want to redirect to custom wishlist page if set path as 'blog/wishlist'

So any idea how to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.