:::: MENU ::::
Monthly Archives: August 2018

Magento 2 get base directory

Today we discuss about Magento 2 get base directory .This tutorial include how to get base directory like media and other directories. There are two ways to do it. One is factory method and other is use object manager. So let start with our example.

Factory Method

In this example we will get base directory of media.

<?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 $directoryList;

public function __construct(
\Magento\Framework\App\Filesystem\DirectoryList $directoryList
) {
    $this->directoryList=$directoryList
}
public function getbaseDir()
{
   return $this->_directorylist->getPath('media');

}

}

Object Manager

In this example we will get base directory of media. Using object manager is not recommended.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$dir = $objectManager->create('Magento\Framework\App\Filesystem\DirectoryList');
$mediaPath = $dir->getPath('media');
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 20, 2018


Magento 2 get media path

Today we discuss about Magento 2 get media path .This tutorial include how to get media path in your block or phtml file. There are two ways to do it. One is factory method and other is use object manager. So let start with our example.

Factory Method

In this example we will get media path. First your need to inject \Magento\Store\Model\StoreManagerInterface.

<?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 $storeManager;

public function __construct(
   
    \Magento\Store\Model\StoreManagerInterface $storeManager,
   
) {
   
    $this->storeManager = $storeManager;
   
}

public function getMediaUrl()
{
    return $mediaUrl = $this->storeManager
                     ->getStore()
                     ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
   
}


}

Object Manager

In this example we will get media path. Using object manager is not recommended.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 20, 2018


Magento 2 disable cache block

Today we discuss about Magento 2 disable cache block .This tutorial include how to disable cache for block. There are two ways to do it. One is disable cache from layout xml for block or disable cache for block is programatically. So let start with our example.

layout cache disable

You can use cacheable=”false” attribute in your layout to disable cache for block but problem is that it will disable whole page cache.

<block class="QaisarSatti\HelloWorld\Block\HelloWorld" name="helloworld" cacheable="false" />

Another option for cache diable

<block class="QaisarSatti\HelloWorld\Block\HelloWorld" name="helloworld"  ttl="30" />

programmatically cache disable

Now the second option is disable cache for block programmatically.

<?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
{
public function getCacheLifetime()
    {
        return null;
    }

}
Author: Qaisar Satti
Category: Magento 2
Last Modified: September 24, 2018


Magento 2 uninstall Module

Today we discuss about Magento 2 uninstall module.This tutorial include how to uninstall or remove or delete module or extension completely from magento 2, uninstall or remove or delete magento 2 module or extension manually. So let start with our example.

Uninstall module with Command

First example will be how to delete or uninstall a module from command line. It will only work when you install a module from composer.

php bin/magento module:disable QaisarSatti_HelloWorld --clear-static-content
php bin/magento module:uninstall QaisarSatti_HelloWorld -r
composer update
php bin/magento setup:upgrade

Uninstall module manually

You can delete or uninstall module manually following below step. For example my module name is QaisarSatti_HelloWorld.

  • Delete the Module folder QaisarSatti from app\code
  • Remove module QaisarSatti_HelloWorld entry from setup_module table

Now run following command.

php bin/magento setup:upgrade
php bin/magento cache:flush
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 17, 2018


Magento 2 get order information by increment id

Today we discuss about Magento 2 get order information by increment id.This tutorial include how to change or get information of order by order increment id, how to load order by increment id. There are three ways to do that Api Repository, Factory method and object manager. So let start with our example.

Factory Method:

Best practice method always use this method

namespace QaisarSatti\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
 protected $orderFactory;
public function __construct(

        \Magento\Sales\Model\OrderFactory $orderFactory,

    ) {


        $this->orderFactory = $orderFactory;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);

}


}

Api Repository:

This was introduce in Magento 2.1 by magento.

namespace QaisarSatti\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
 {
     protected $order;
public function __construct(

\Magento\Sales\Api\Data\OrderInterface $order,

    ) {


        $this->order = $order;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->order->loadByIncrementId($orderIncrementId);

}
}

Object Manager:

Sometime you need to load order record in phtml file. Not recommended method to use in magento2 but so magento use it so here it is just for knowledge.

$orderIncrementId=10000003;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInfo = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId);
Author: Qaisar Satti
Category: Magento 2
Last Modified: October 4, 2018


Magento 2 Create CMS Block

Today we discuss about Magento 2 create cms block.This tutorial include how to add new cms block or static block from admin panel, create cms block programmatically. So let start with our example.

Cms block in admin panel

Login to admin panel

Content > Elements > Block

Magento 2 Create CMS Block

Cms block in programmatically

We are creating the cms static block in our installer script. We use block factory to create static block.

namespace QaisarSatti\HelloWorld\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface {
    private $blockFactory;

    public function __construct(
    \Magento\Cms\Model\BlockFactory $blockFactory
     )
    {

    $this->blockFactory = $blockFactory;
    }  
    public function install( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
        $installer = $setup;

        $installer->startSetup();

       $testBlock = [
         'title' => 'Test block title',
         'identifier' => 'test-block',
         'stores' => [0],
         ‘Content’ => ‘Your content here’
         'is_active' => 1,
         ];
        $this->blockFactory->create()->setData($testBlock)->save();

        $installer->endSetup();
    }
}
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 10, 2018


Magento 2 set homepage

Today we discuss about Magento 2 set homepage.This tutorial include how to change or set homepage in system configuration program and how to change or set homepage from admin panel system configuration.There are two way to set home page one is from admin panel other is set homepage programmatically. So let start with our example

Set homepage from Admin Panel

Login to admin panel.

Store > Setting > configuration > Web Tab > Default Page

Select your desired page from dropdown.

Field CMS Home Page

Magento 2 set homepage

Set homepage from programmatically

Just inject the class \Magento\Config\Model\ResourceModel\Config $_resourceConfig and set value for web/default/cms_home_page to desired page ‘youridentifier’.

public function __construct(\Magento\Config\Model\ResourceModel\Config $_resourceConfig)
    {
        $this->_resourceConfig = $_resourceConfig;
    }

        $this->_resourceConfig->saveConfig('web/default/cms_home_page', 'youridentifier', 'default', 0);
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 10, 2018


Pages:12