<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magenticians_Mymodule" setup_version="1.0.1"></module> </config>Next, create registration.php in app/code/Magenticians/Mymodule and add this code to the file
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magenticians_Mymodule', __DIR__ );At this point, I can direct use Object Manager in the module addtocart.phtml to check the session of the user. However, this is not the right approach because it wastes too many server resources on unnecessary calls. I did some research and got inspired by Frank Clark’s Guide
<?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="add_layout_handles" instance="Magenticians\Mymodule\Observer\AddHandles" /> </event> </config>Next, create AddHandles.php in app/code/Magenticians/Mymodule/Observer and add this code to the file:
<?php namespace Magenticians\Mymodule\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Customer\Model\Session as CustomerSession; class AddHandles implements ObserverInterface { protected $customerSession; public function __construct(CustomerSession $customerSession) { $this->customerSession = $customerSession; } public function execute(\Magento\Framework\Event\Observer $observer) { $layout = $observer->getEvent()->getLayout(); //To check that customer is logout if (!$this->customerSession->isLoggedIn()) { $layout->getUpdate()->addHandle('customer_logged_out'); } } }In the above code, you can see the constructor where I have added customer session. Then, in the execute() function, I added the handle customer_logged_out to the layout. To use this handle, create a layout file customer_logged_out.xml in app/code/Magenticians/Mymodule/view/frontend/layout and add this code to it:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="product.info.addtocart"> <action method="setTemplate"> <argument name="template" xsi:type="string">Magenticians_Mymodule::catalog/product/view/addtocart.phtml</argument> </action> </referenceBlock> </body> </page>
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ // @codingStandardsIgnoreFile /** @var $block \Magento\Catalog\Block\Product\View */?> <?php $_product = $block->getProduct(); ?> <?php if ($_product->isSaleable()): ?> <div class="box-tocart"> <p>Please <a href="<?php echo $block->getUrl('customer/account/login') ?>" title="<?php echo __('Login') ?>"><?php echo __('Login') ?></a> or <a href="<?php echo $block->getUrl('customer/account/create') ?>" title="<?php echo "Register" ?>"><?php echo "Register" ?></a> to buy this product!</p> </div> <?php endif; ?>
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy php bin/magento cache:clean php bin/magento cache:flush
Your greatest possible competitive advantage can be your clients and the interactions they have with… Read More
Digital marketing KPIs are measurable values that marketing teams use to track progress toward desired… Read More
In today's digital age, fraud poses a significant threat to businesses of all sizes. As… Read More
Financial crimes continue to evolve and proliferate in our increasingly digital, global economy. From complex… Read More
In the highly competitive modern workplace, trust, and employee loyalty are crucial factors for long-term… Read More
In the ever-evolving world of small business developing and implementing effective marketing strategies is critical to… Read More
View Comments
Thanks for the article
I am having issue
Fatal error: Uncaught TypeError: Argument 1 passed to AdnanHidePricesObserverAddHandles::__construct() must be an instance of AdnanHidePricesObserverMagentoCustomerModelSession, instance of MagentoFrameworkObjectManagerObjectManager given, called in /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93 and defined in /var/www/html/app/code/Adnan/HidePrices/Observer/AddHandles.php:14 Stack trace: #0 /var/www/html/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(93): AdnanHidePricesObserverAddHandles->__construct(Object(MagentoFrameworkObjectManagerObjectManager)) #1 /var/www/html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(88): MagentoFrameworkObjectManagerFactoryAbstractFactory->createObject('Adnan\HidePrice...', Array) #2 /var/www/html/vendor/magento/framework/ObjectManager/ObjectManager.php(71): MagentoFrameworkObjectManagerFactoryCompiled->create('Adnan\HidePrice...') #3 /var/www/html/vendor/magento/framework/Event/Obser in /var/www/html/app/code/Adnan/HidePrices/Observer/AddHandles.php on line 14
The problem is in the Observer instantiator in the file
/etc/frontend/events.xml
change: / for:
Thanks for highlighting, post is updated!
thanks to you for sharing this tutorial, greetings.