0

How can I hide product "Summary" for guest visitors when they click on View and Edit cart? Or just disable View and Edit from cart for guest visitors unless they login?

enter image description here

7
  • Please share screen shots and explain properly what your need.
    – ZealousWeb
    Jun 24, 2021 at 4:55
  • I have edited my question with screenshot. How can I disable it from admin panel?
    – Ben
    Jun 24, 2021 at 5:00
  • do you want to restrict cart and checkout page for guest user?
    – ZealousWeb
    Jun 24, 2021 at 5:06
  • I want guest to add product but when they click on "View and Edit cart" they should not see Summary like in the picture, or just hide "View and Edit cart" from guest. When they login then only they can view summary etc. Please share both ways.
    – Ben
    Jun 24, 2021 at 5:10
  • or let's say when guest user click on "View and Edit cart" then they must be redirected to login/registeration
    – Ben
    Jun 24, 2021 at 5:43

1 Answer 1

0

I am showing the way "when guest user click on "View and Edit cart" then they must be redirected to login/registeration"

You can do with the use of events. Please add below events and observer code in your custom module.

step 1: Please create file events.xml under path PackageName/Module/etc/frontend/

Write below code

<?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="layout_load_before">
        <observer name="redirectcustomer_success" instance="PackageName\Module\Observer\RedirectCustomer" />
    </event>
</config>

Step 2: Please create file RedirectCustomer.php under path PackageName/Module/Observer/

<?php
namespace PackageName\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\Http as HttpResponse;

class RedirectCustomer implements ObserverInterface
{
     /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    protected $responseFactory;

    protected $_customerSession;

    protected $_customerUrl;

    protected $_urlInterface;

    protected $request;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->responseFactory = $responseFactory;
        $this->_customerSession = $customerSession;
        $this->_customerUrl = $customerUrl;
        $this->_urlInterface = $urlInterface;
        $this->request = $request;
    }

    public function execute(Observer $observer)
    {   
        $redirectUrl = $this->getCustomerLoginUrl(); 
        $currentUrl = $this->_urlInterface->getCurrentUrl(); 
        $fullActionName = $this->request->getFullActionName();

        if(!$this->_customerSession->isLoggedIn() && ($fullActionName == 'checkout_cart_index' || $fullActionName == 'checkout_index_index')) {
            $resultRedirect = $this->responseFactory->create();
            $resultRedirect->setRedirect($redirectUrl)->sendResponse('200');
            exit();
        }   
    }

    public function getCustomerLoginUrl() 
    {   
        return $this->_customerUrl->getLoginUrl();
    }
}

Please check and let us know

Cheers!

1
  • I dont have a custom module. where do i add it? i m new to magento2. can u pls guide me?
    – Ben
    Jun 25, 2021 at 9:32

Your Answer

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

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