1

I need to integrate a functionality where when a user clicks add to cart it should check whether he is a logged in customer or not. If he is a logged in customer the product should be added in cart. Else it should redirect to Login page after logging in the products to be added automatically. I am using event/observer according to this article but no luck.

I am using below approach:-

  • Using controller_action_predispatch_checkout_cart_add event to check the customer login status and redirect to login page id customer is not logged in.

  • On customer_login event I am adding the product in cart after customer login.

    above solution works on category page, but not working on product detail page. it give me below error:-

    main.DEBUG: URI "/magento/checkout/cart/add/uenc/aHR0cDovL25lanJlZWRlbW8ud2ViZXBvd2VyLmJpei9tYWdlMjMxYi9mdXJuaXR1cmUuaHRtbA%2C%2C/product/1/" cannot be accessed with GET method (Magento\Checkout\Controller\Cart\Add) [] []

    Request validation failed for action "Magento\Checkout\Controller\Cart\Add\Interceptor"

Please let me know what is the best practice to achieve this according to Magento2 standard.

0

Just disable guest checkout in magento admin.

https://docs.magento.com/m2/ce/user_guide/sales/checkout-guest.html

  1. On the Admin sidebar, click Stores.

  2. In the Settings section, choose Configuration.

  3. In the Sales section in the left panel, choose Checkout.

  4. Expand the Checkout Options section.

    Checkout Options If necessary, clear the Use system value checkbox.

    If the setting is for a specific store view, choose the store view where the configuration applies. When prompted, click OK to continue.

  5. Set Allow Guest Checkout to “No”.

  6. Click Save Config.

  • Hi MagenX, This will not solve his issue. He is asking whenever I am clicking on "Add To cart", it should prompt for login, but the method you gave will only ask for login when you do the checkout not add to cart. – Rahul Anand Sep 2 at 10:50
  • this is a very simple way to request a login without any garbage in the code ... – MagenX Sep 2 at 11:14
  • Absolutely correct MagenX but his requirement is different . The settings you mentioned will work on checkout not on add to cart. – Rahul Anand Sep 2 at 11:15
0

1) etc/frontend/events.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
        <event name="controller_action_postdispatch">
            <observer name="customer_controller_action_postdispatch_event" instance="Custom\Module\Observer\ForceCustomerLoginObserver" />
        </event>
</config>

2) Custom/Module/Observer/ForceCustomerLoginObserver.php

    namespace Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ForceCustomerLoginObserver implements ObserverInterface
{
    protected $responseFactory;

    protected $url;

    private $scopeConfig;

    private $customerSession;

    private $customerUrl;

    private $context;

    private $contextHttp;

    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\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;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $isLoggedIn = $this->contextHttp->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

        if ($isLoggedIn ) {
            return $this;
        }
        $this->messageManager->addSuccess(__('You must login into system for accessing this page.'));
        $customRedirectionUrl = $this->url->getUrl('customer/account/login'); 
        $this->responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse(); 
        die();
    }
}
  • After implement this solution, page redirected to "The page isn’t redirecting properly" page. – Purushotam Sharma Sep 2 at 12:21
  • I have implement solution just look it. – Arvind Hathiya Sep 3 at 11:49

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.