:::: MENU ::::
Browsing posts in: Magento 2

Magento 2 create  and use helper

Using helper you can put your methods in there that you have no idea where to put them. For example if you want create the thumbnail of image you use the helper method. The method you don’t know where to put put in helper class.

Now creating the helper and use of it.

Create Helper folder in module directory.



namespace QaisarSatti\HelloWorld\Helper

 

Now create the your helper class what name you want to put.In my case i will use Data file. Now the complete path will be.

namespace QaisarSatti\HelloWorld\Helper\Data.php



This file will be extend with.



 \Magento\Framework\App\Helper\AbstractHelper



Now create helper Data class.

 


<?php

/**

* Simple Hello World Module

*

* @category QaisarSatti

* @package QaisarSatti_HelloWorld

* @author Muhammad Qaisar Satti

* @Email [email protected]

*

*/


namespace QaisarSatti\HelloWorld\Helper;



use Magento\Store\Model\Store;



class Data extends \Magento\Framework\App\Helper\AbstractHelper

{



public function getTitle()

{

return  'First Hello World Module';

}

}

Now we will use helper method  in our block.

<?php

/**

* Simple Hello World Module

*

* @category QaisarSatti

* @package QaisarSatti_HelloWorld

* @author Muhammad Qaisar Satti

* @Email [email protected]

*

*/


namespace QaisarSatti\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template

{

protected $_helper;

public function __construct(

\QaisarSatti\HelloWorld\Helper\Data $_helper,

) {



$this->_helper=$_helper;



}

public function _prepareLayout()

{

parent::_prepareLayout();

$this->pageConfig->getTitle()->set($this->_helper->getTitle());

return $this;

}

}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 set meta tag

Every Magento project requires a certain level of customization. This may involve adding a new element or overriding an existing one. Today, we’ll talk about how to set Meta tag like Meta title Meta Keyword and Meta description programmatically in Magento 2.For any suggestions & question, please feel free to drop a comment.

 

Block


$this->pageConfig->getTitle()->set(__('First Hello World Module')); //setting the page title
$this->pageConfig->setDescription('test description'); // set meta description
$this->pageConfig->setKeywords('test keywords'); // set meta keyword

 

Controller

 

namespace QaisarSatti\HelloWorld\Controller\Index;

use Magento\Framework\View\Result\PageFactory;

class Index extends Action {

protected $_resultPage;

public function __construct(
PageFactory $resultPage) {
$this->_resultPage = $resultPage;
}
public function execute() {
$result= $this->_resultPage->create();
$result->getConfig()->getTitle()->set("Meta Title"); //setting the page
$result->getConfig()->setDescription("Description"); // set meta description
$result->getConfig()->setKeywords("Key Words"); // set meta keyword

return $result;
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


magento 2 Remove layered navigation

Today we talk about how in magento 2 Remove layered navigation. If you want to remove for for all the categories then add this code in your default.xml

Add this to your theme or module

 

default.xml

<referenceBlock name="catalog.leftnav" remove="true" />

 

If you want to do this for single category you can do that from admin panel. Login to admin panel and then go to

Admin Panel > Products > Categories > YOUR PAGE > Design

 

Add the following xml.

<referenceContainer name="catalog.leftnav" remove="true" />
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 get static url or skin url

Today we talk about how to Magento2 get view or skin url in you phtml file. For example you image is located in folder web/images. simply add below code with image path and image name.

<?php echo $block->getViewFileUrl('images/test.png') ?>

 

If you want to add in cms page simple add with image path.

{{view url='images/test.png'}}

If you want to get specific module image in web folder. In my case Module name QaisarSatti_HelloWorld

<?php echo $block->getViewFileUrl('QaisarSatti_HelloWorld::images/test.png'); ?>

If you want to get image url in your block file.

$this->getViewFileUrl('QaisarSatti_HelloWorld::images/test.png');

If you want to see where   $block->getViewFileUrl()  method define. You will see every Magento\Framework\View\Element\Template file extends from Magento\Framework\View\Element\Template that extends with Magento\Framework\View\Element\AbstractBlock.php.

you can View on line 762 this method getViewFileUrl is define.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 remove any link from top links

 

Today we talk about how in Magento 2 remove any link from top links. For removing the top-link you must know name of the link to remove link. Register-link is for register-link link.

Here is  Name list of top-link.



register-link                Create Account Link

authorization-link       Sign In Link

 

wish-list-link              WishList Link

 

my-account-link        My Account Link

 

Add below code in your default.xml remove the link. For example you want to remove the Create Account Link for that you have to add below xml.

<referenceBlock name="top.links">
        <referenceBlock name="register-link" remove="true" />          
 </referenceBlock>

 

Same goes for other link too.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 Override default theme template files

If you are doing the custom theme then you must know how in Magento 2 Override default theme template files in your custom theme.You want to override all the files then simply go to.

vendor/magento/module-theme/view/frontend/

And copy the templates folder in your theme. In my case theme name is theme. So in my theme folder create the Magento_Theme Folder. And paste the templates folder there. Now your theme structure will look like this.

app/design/frontend/QaisarSatti/theme/Magento_Theme/templates

If you want override the single file like header.phtml. Then copy file from

vendor/magento/module-theme/view/frontend/templates/html/header.phtml

Paste in your theme. And file structure will look like

app/design/frontend/QaisarSatti/theme/Magento_Theme/templates/html/header.phtml
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Pages:1...56789101112