1

Trying to create a module that redirects the customer to the account creation page when they add to cart if they are not logged in, I have the following code in app/code/Mason/CheckoutRedirect/Plugin/Checkout/Controller/Cart/Add.php:

namespace Mason\CheckoutRedirect\Plugin\Checkout\Controller\Cart;

class Add
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $url;

    /**
     * Data constructor.
     *
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
     * @param \Magento\Framework\UrlInterface $url
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->objectManager = $objectManager;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->url = $url;
    }

    public function aroundExecute(
        \Magento\Checkout\Controller\Cart\Add $subject,
        \Closure $proceed
    ) {
        $contactUrl = $this->url->getUrl('customer/account/create');
        $result = [];
        $result['backUrl'] = $contactUrl;

        $subject->getResponse()->representJson(
            $this->objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result)
        );
        return;
    }
}

All this is doing is returning a JSON object:

{"backUrl":"https://domain.com/customer/account/create/"}

But it isn't redirecting. I think I might be accidentally over riding the whole class rather than extending it, but I'm not sure what to change to fix it.

1 Answer 1

0

Try following way:

class Add
{
    /** @var \Magento\Customer\Model\Session */
    protected $customerSession;

    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlManager;

    /**
     * @var \Magento\Framework\Json\Helper\Data
     */
    protected $jsonHelper;

    /**
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
     * @param \Magento\Framework\UrlInterface $url
     * @param \Magento\Framework\Json\Helper\Data $jsonHelper
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\UrlInterface $urlManager,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->customerSession = $customerSession;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->urlManager = $urlManager;
        $this->jsonHelper = $jsonHelper;
    }

    public function aroundExecute(
        \Magento\Checkout\Controller\Cart\Add $subject,
        \Closure $proceed
    ) {

        if(!$this->customerSession->isLoggedIn()) {
            if (!$subject->getRequest()->isAjax()) {
                $resultRedirect = $this->resultRedirectFactory->create();
                $resultRedirect->setUrl('customer/account/create');
                return $resultRedirect;
            } else {
                $result = [];
                $result['backUrl'] = $this->urlManager->getUrl('customer/account/create');
                return $subject->getResponse()->representJson(
                    $this->jsonHelper->jsonEncode($result)
                );
            }
        }

        return $proceed();
    }
}

Your Answer

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

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