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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityLike 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
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
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.