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

  • did you override the controller? – Fme Extensions Dec 5 '16 at 6:54
0

Yes, it's getting the plugin correctly, but it only returns the JSON object, rather than doing the redirect that I want it to do

Your Answer

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

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