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

list all customer groups in Magento 2

Today we will learn how to list all customer group in magento 2. Getting the complete list of customers/users means displaying a list of all the customer groups including not logged in group as well.

So,you can get the complete list of customer groups,using following code:

Factory Method:

namespace QaisarSatti\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $groupCollection;  


  public function __construct(
     
        \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollection

    ) {


        $this->groupCollection = $groupCollection;
     
    }
    public function getCustomerGroup()
    {
        $product_id=7;
        return $this->groupCollection->create()->toOptionArray();
    }

}

Object Manager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupOptions = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();

$fieldset->addField(
    'customer_group',
    'multiselect',
    [
        'name' => 'customer_group[]',
        'label' => __('Customer Group'),
        'title' => __('Customer Group'),
        'values' => $groupOptions,
        'disabled' => $isElementDisabled
    ]
);

One can also use the following class:

Magento\Customer\Model\Customer\Source\Group

This class provides the

toOptionArray

method which can be used to get an array of customer groups.

That’s it for this tutorial. I hope it will help you in a certain and easy way.

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


Magento 2 admin panel Unable to sign in or Account temporarily disabled

Today we will discuss about how you can solve the issue of magento 2 admin panel unable to sign in or account temporarily disabled. This mainly occurs due to secure functionality of magento 2. Magento 2 have introduced account disabled functionality keeping in mind the security of business. if you tried the wrong password repeatedly in admin panel login. Then you account got locked and temporary disabled. There are various methods to do so but we will discuss few here.

Lets have a look at the first method, in which you will learn to unlock and access the root folder

Unlocking and accessing ROOT folder

One can unlock the temporarily disabled account by going to Magento 2 root folder SSH and running the
the following command:

php bin/magento admin:user:unlock admin

Another way of doing so is by using magento 2 root directory

Magento 2 root directory

Jump into bin directory from magento 2 root: cd bin
and then run the following command

php magento admin:user:unlock admin

There is another way of solving this problem but that might work for some and might not work for others. But is worth giving a try.

By resetting the username and password

For doing so do the following

sudo php bin/magento admin:user:create --admin-user="pearlbells" --admin-password="pear122l**all" --admin-email="[email protected]" --admin-firstname="Admin" --admin-lastname="Admin"

As i told you earlier there are many ways but we just discussed few simple and efficient methods.I Hope they will serve the purpose ad be useful.

Author: Qaisar Satti
Category: Magento 2
Last Modified: September 24, 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')->load($orderIncrementId);
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 17, 2018


Pages:1234567...15