<?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>Add registration.php in app/code/Magenticians/Mymodule and copy the following code in it:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magenticians_Mymodule', __DIR__ );
<?php namespace Magenticians\Mymodule\Helper; use Magento\Framework\App\Helper\AbstractHelper; class Data extends AbstractHelper { /** * @var \Magento\Framework\App\Http\Context */ private $httpContext; public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Http\Context $httpContext ) { parent::__construct($context); $this->httpContext = $httpContext; } public function isLoggedIn() { $isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); return $isLoggedIn; } }In the code (helper) above, I have created a function isLoggedIn() to get the status of logged in users.
<?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>I have created the above file to override the core addtocart file with our custom addtocart file. To show the login and register option to the guest users, go to addtocart.phtml from app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view and and copy the following code in it:
<?php $helper = $this->helper('Magenticians\Mymodule\Helper\Data');?> <?php if($helper->isLoggedIn()): ?> <div class="box-tocart"> <p><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></p> </div> <?php endif; ?>In the above code, I have set the condition that if the user is not logged in, show the register and login links. The final addtocart.phtml will be:
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */// @codingStandardsIgnoreFile /** @var $block \Magento\Catalog\Block\Product\View */?> <?php $helper = $this->helper('Magenticians\Mymodule\Helper\Data');?> <?php if($helper->isLoggedIn()): ?> <div class="box-tocart"> <p><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></p> </div> <?php endif; ?> <?php $_product = $block->getProduct(); ?> <?php $buttonTitle = __('Add to Cart'); ?> <?php if ($_product->isSaleable()): ?> <div class="box-tocart"> <div class="fieldset"> <?php if ($block->shouldRenderQuantity()): ?> <div class="field qty"> <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label> <div class="control"> <input type="number" name="qty" id="qty" value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>" title="<?= /* @escapeNotVerified */ __('Qty') ?>" class="input-text qty" data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>" /> </div> </div> <?php endif; ?> <div class="actions"> <button type="submit" title="<?= /* @escapeNotVerified */ $buttonTitle ?>" class="action primary tocart" id="product-addtocart-button"> <span><?= /* @escapeNotVerified */ $buttonTitle ?></span> </button> <?= $block->getChildHtml('', true) ?> </div> </div> </div> <?php endif; ?> <?php if ($block->isRedirectToCartEnabled()) : ?> <script type="text/x-magento-init"> { "#product_addtocart_form": { "Magento_Catalog/product/view/validation": { "radioCheckboxClosest": ".nested" } } } </script> <?php else : ?> <script type="text/x-magento-init"> { "#product_addtocart_form": { "Magento_Catalog/js/validate-product": {} } } </script> <?php endif; ?>
rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* var/session/* var/view_preprocessed/* pub/static/* php bin/magento setup:upgrade php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:clean php bin/magento cache:flushNow go to the product page of your store and logged in as a customer, you will see the desired result:
Finding The Righteous Hosting For Magento Store?
With Cloudways, you can avail FREE SSL, Migration and much more in just a few clicks.
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