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

Magento 2 get set unset session

Today we talk about how in Magento 2 get, set, and unset session. This tutorial include set session, get session and unset the session. Sometimes in customization and module development we need to get the session information , that how long a user remains on our site or we may need to unset the session given the certain amount of time . Following piece of code will let us to play with the global session variable to achieve our intended goals.

protected $coreSession;

public function __construct(
   
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){

    $this->coreSession = $coreSession;
   
}

public function setSession(){
    $this->coreSession->start();
    $this->coreSession->setTest('The Core session');
}

public function getSession(){
    $this->coreSession->start();
    return $this->coreSession->getTest();
}

public function unSetSessione(){
    $this->coreSession->start();
    return $this->coreSession->unsTest();
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 Current Store Date Time

Today we talk about how in Magento 2 Current Store Date Time. Following this example you can get store date and time in block, helper, controller or model too. So let start with our example.
You need to inject in your class constructor an instance of Magento\Framework\Stdlib\DateTime\DateTime.

protected $date;
public function __construct(
   
    \Magento\Framework\Stdlib\DateTime\DateTime $date,

) {
   
    $this->date = $date;

}

Then, you can use in your class this:

$date = $this->date->gmtDate();
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 Set Block template

Today we talk about how in Magento 2 Set Block template. This tutorial included set block template in layout file, block file . Following this example you can get store date and time in block, helper, controller or model too. So let start with our example.

In layout File

    <block class="QaisarSatti\HelloWorld\Block\HelloWorld" name="HelloWorld" template="QaisarSatti_HelloWorld::HelloWorld.phtml"></block>

In Block File

Used the _construct() method to set template.

protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('QaisarSatti_HelloWorld::HelloWorld.phtml');
    }

Another way to set template in block in set the $_template variable to your template.

protected $_template = 'QaisarSatti_HelloWorld::HelloWorld.phtml';
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 product count by attribute

Today we talk about how in Magento 2 product count by attribute. This tutorial included get product count by product attribute values. Following this example you can get count product by any attribute. For using this example you just need to replace you_attribute_code with you attribute code i.e color. So let start with our example.

    protected $_productCollectionFactory;
    protected $attributFactory;
    protected $eavConfig;

    public function __construct(
   
        \Magento\Catalog\Model\ResourceModel\Product $productFactory,
        \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attributFactory,
        \Magento\Eav\Model\Config $eavConfig,

       
    ) {
   
        $this->eavConfig = $eavConfig;
        $this->attributFactory = $attributFactory;
        $this->_productCollectionFactory = $productFactory;            

    }


    public function getProductCountByAttributeCode()
     {
        $attributeCode = ‘you_attribute_code’;
        $attribute = $this->eavConfig->getAttribute('catalog_product', $attributeCode);
        $attributFactory = $this->attributFactory->create()->setStoreFilter(0, false);
         $itemCollection = $this->_productCollectionFactory;
        $attributFactory->getSelect()
        ->joinLeft(
            array('value_table' => $itemCollection->getTable('catalog_product_entity_int')),
            'main_table.option_id=value_table.value AND main_table.attribute_id=value_table.attribute_id', 'entity_id')
        ->reset(\Zend_Db_Select::COLUMNS)
        ->columns(array('main_table.option_id',new \Zend_Db_Expr('COUNT(value_table.entity_id)')))
        ->where('main_table.attribute_id=:attribute_id')
        ->group('main_table.option_id');
        $result = $itemCollection->getConnection()->fetchPairs(
        $attributFactory->getSelect(), array('attribute_id' => $attribute->getId()));
        return $result;        
    }

Note: Using above example you can count product by any attribute.

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


Magento 2 remove page title

Today we talk about how in Magento 2 remove page title. In this tutorial we will learn how to remove page title that are shown under top menu navigation. We will remove the page title by xml layout file. If you want to remove on all page create a default.xml or you want to remove title for a specific page for example product view page catalog_product_view.xml. So let’s start with our example to remove for all pages.

Remove Title From all Pages in Module

First create the default.xml in QaisarSatti/HelloWorld/view/frontend/layout/

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="page.main.title" remove="true" />
    </body>
</page>

Remove Title From all Pages in Theme

First create the default.xml in QaisarSatti/theme/Magento_Theme/layout/

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="page.main.title" remove="true" />
    </body>
</page>
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 retrieve admin url

Today we talk about how in Magento 2 retrieve admin url. This tutorial included how to get admin url in case you forgot about that. This tutorial show how can you get your admin url from command and also from file. So let start with our example.

File system:

You can also retrieve the the Magento 2 Admin URL in /app/etc/env.php. Example of the relevant file content:

'backend' =>
    array (
       'frontName' => 'admin_test',
    ),

Using Command Line Interface(CLI):

Run the following command to retrieve admin url information.

php bin/magento info:adminuri
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 set layout controller

Today we talk about how in Magento 2 set layout controller. You can set any layout in your controller file. In this example we are setting 3columns layout in our controller. You can set any layout your own controller. There is list of available layout in magento 2.

Available Layout

List of layout you can set in your controller.

1column
2columns-left
2columns-right
3columns
empty

Set Template in controller

Magento 2 using setPageLayout to setting the layout of current page.

public function execute()
{

     $page = $this->resultPageFactory->create(false, ['isIsolated' => true]);          

     $customLayout = '3columns';

     $page->getConfig()->setPageLayout($customLayout);
     return $page;
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Pages:12345678...15