0

Like the title says, those buttons look terrible when customers aren't logged in, and the functionality ins't needed, so how can I disable favorites and compare for people who aren't logged in?

Thanks

1

2 Answers 2

1

We can disable Add to cart and Wishlist for Not Logged in User by "layout_generate_blocks_after" event

Create these files in a custom module.

etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_generate_blocks_after">
    <observer name="update_layout" instance="Bytepattern\RemoveBlock\Observer\UpdateLayout" />
</event>
</config>

Observer/UpdateLayout.php

<?php

namespace Bytepattern\RemoveBlock\Observer;

class UpdateLayout implements \Magento\Framework\Event\ObserverInterface
{

private $httpContext;

public function __construct(
    \Magento\Framework\App\Http\Context $httpContext
) {
    $this->httpContext = $httpContext;
}

/**
 * Execute observer.
 * @param \Magento\Framework\Event\Observer $observer
 * @return $this
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{

    $layout = $observer->getLayout();

    if(!$this->isLoggedIn()) {
        // Customer is not logged in
        $layout->unsetElement('product.info.addtocart');
        $layout->unsetElement('wish-list-link');
        $layout->unsetElement('minicart');
        
     } 

    return $this;
}

public function isLoggedIn()
{
    $isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    return $isLoggedIn;
}
}

Note: Replace Bytepattern/RemoveBlock with your Vendor/Module

0

By default, there is no way to disable wishlist and add to the cart for only not-logged in customers. You can only disable totally wishlist

In my case, I was already been struggled to disable add to cart for not logged in but finally, came up with some additional extensions.

Your Answer

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

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