I'm trying redirect to the login when some guess user press the add cart button and he is not registered. But i can't do it. I am usin a Observer, with the following event
<event name="controller_action_predispatch_checkout_cart_add">
this is my /app/code/vendor/module/etc/frontend/event.xml
event.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="controller_action_predispatch_checkout_cart_add">
<observer name="restrict_sales_model_cart_add_before" instance="vendor\module\Observer\ForceLogin" />
</event>
</config>and my Observer/app/code/vendor/module/Observer/ForceLogin.php
ForceLogin.php
<?php
namespace vendor\module\Observer;
use Magento\Framework\Event\ObserverInterface;
class ForceLogin implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
private $scopeConfig;
private $customerSession;
private $customerUrl;
private $context;
private $contextHttp;
protected $redirect;
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $contextHttp, \Magento\Customer\Model\Url $customerUrl, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Framework\App\Response\RedirectInterface $redirect, \Magento\Framework\UrlInterface $url)
{
$this->scopeConfig = $scopeConfig;
$this->context = $context;
$this->customerSession = $customerSession;
$this->customerUrl = $customerUrl;
$this->contextHttp = $contextHttp;
$this->messageManager = $context->getMessageManager();
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->redirect = $redirect;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$isLoggedIn = $this
->contextHttp
->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if (!$isLoggedIn)
{
$this
->messageManager
->addError(__('Loggin First.'));
$customRedirectionUrl = $this
->_url
->getUrl('customer/account/login');
$this
->_responseFactory
->create()
->setRedirect($customRedirectionUrl)->sendResponse('200');
exit();
}
return $this;
}
}Any idea why he is not doing the redirect?
Hello @ignacio_m
Why don't you check out these?
Compare code or use same extension.
https://github.com/bitExpert/magento2-force-login?files=1
https://github.com/abdelkebir/magento2-force-login?files=1
Hello @ignacio_m
check default wishlist functionality and implement that way
Hello @theMageComp
Thanks, but not working for me
I still can't make him redirect when press add the cart button. Maybe it's for my event?
@theMageComp wrote:Hello @ignacio_m
Why don't you check out these?
Compare code or use same extension.
https://github.com/bitExpert/magento2-force-login?files=1
https://github.com/abdelkebir/magento2-force-login?files=1
Will you please remove exit; from yours, if condition and then try again.
Not working, this is my new fuction. I think my event it's wrong:
<event name="controller_action_predispatch_checkout_cart_add">
New function.
public function execute(\Magento\Framework\Event\Observer $observer)
{
$actionName = $observer->getEvent()->getRequest()->getFullActionName();
if($actionName == 'checkout_cart_add'){
if(!$this->_customerSession->isLoggedIn())
{
$this->_messageManager->addError('es distinto.');
$observer->getRequest()->setParam('product', false);
$redirectionUrl = $this->_url->getUrl('customer/account/login');
$this->_responseFactory->create()->setRedirect($redirectionUrl)->sendResponse();
}
}else{
return $this;
}
return $this;
}
@ignacio_m
why can't you set the guest checkout to no,
store->configuration->sales->checkout->checkout option
if it work give kudos and accept as solution.