What Is Magento 2 Helper?
We use Helpers in Magento 2 class for adding functionalities to several features and we can use those functionalities anywhere in the entire Magento store. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Webiators/Mymodule/etc and copy the following code in it:
1 2 3 4 5 |
<?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="Webiators_Mymodule" setup_version="1.0.1"> </module> </config> |
Add registration.php in app/code/Webiators/Mymodule and copy the following code in it:
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Webiators_Mymodule', __DIR__ ); |
Create Magento 2 Helper
So now add Data.php file in app/code/Webiators/Mymodule/Helper and copy the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace Webiators\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, As you can see i have created a function isLoggedIn() to get the status of logged in users.
Override addtocart.phtml
So now go to vendor/magento/module-catalog/view/frontend/templates/product/view from the root directory of your store and you will see the addtocart.phtml file. Copy the file and paste it into app/code/ Webiators /Mymodule/view/frontend/templates/catalog/product/view.
Add catalog-product.view.xml file in app/code/Webiators/Mymodule/view/frontend/layout and copy the following code in it:
1 2 3 4 5 6 7 8 9 10 11 |
<?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">Webiators _Mymodule::catalog/product/view/addtocart.phtml</argument> </action> </referenceBlock> </body> </page> |
so let’s create 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/Webiators/Mymodule/view/frontend/templates/catalog/product/view and copy the following code in it:
1 2 3 4 5 6 7 8 |
<?php $helper = $this->helper('Webiators\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.
So the final addtocart.phtml will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?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('Webiators\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; ?> |
Run CLI Commands
Launch SSH terminal and go to the root directory of your Magento 2 store.
Then run the following commands:
1 2 3 4 5 6 |
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 |
So now go to the product page of your Magento website and you will see the result:
And Finally you can see that Magento 2 helper is working.
Looking For Magento Ecommerce specialist to help you with your requirements
Start A Project