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.