Category Archives: Magento2 Faq

Magento 2 get system config value


Magento 2 get system config value Magento System.xml configuration values are saved in core_config_data table. There are many ways to get the configuration values in magento 2. Here in this article we are going to explain how you can get the configuration values.


Magento 2 get system config value In Controller | Helper | Model | Block | System Configuration Values | Example

You can get the configuration values in following ways-

\Magento\Framework\App\Config\ScopeConfigInterface class is used in constructor argument to get the configuration values simply as below-

Magento 2 get system config value Example:

resultPageFactory = $resultPageFactory;        
        $this->_scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }

    public function getConfigValue(){
       $myconfig = $this->_scopeConfig->getValue('section/group/field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
}
?>

In the above example we have created method – getConfigValue() to get the configuration value where you need to pass the section, group, field id and store scope. You can use the same method to get the configuration in Model, Blocks and Helpers as well.

Magento 2 get system config value in Phtml

Using object manage you can get system config value on phtml files. Here is an example of system config value on phtml file.

Magento 2 get system config value Example:

get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('group/field/value');
?>

So using the object manager you can get config values anywhere in your Module.

Magento 2 Get Current Customer Id


Magento 2 Get Current Customer Id It very common we need to get the current customer’s detail. The way to get the current customer detail is different than the mangento 1. Here in this tutorial we are going to explain how you can get current customer id in Magento 2.


Magento 2 Get Current Customer Id Example

You can use \Magento\Customer\Model\Session to get the current customer id. First import this class to get the customer session, after this you will be able to get the customer session data. Here is an example how to get the customer id from customer session –

Magento 2 Get Current Customer Id Example:

resultPageFactory = $resultPageFactory;        
        $this->_customerSession = $customerSession;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }

    public function getCustomer(){
        echo $this->_customerSession->getCustomer()->getId(); //Get Current customer ID
        $customerData = $this->_customerSession->getCustomer(); //Get Current Customer Data
        print_r($customerData->getData());


    }
}
?>

In the above example we have created a simple controller method getCustomer() to get the customer details- $this->_customerSession->getCustomer()->getId(); will give you current customer id, You can also get customer’s other details $customerData->getData().

You can get the current customer details in block, model or helper using the above method.

Other Method

You can also use object manger to get the current logged in customer detail. You can fetch the customer session data simply as below –

Get Customer Id, Email, Customer Group, And Name Example:

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    if($customerSession->isLoggedIn()) {
        echo   $customerSession->getCustomer()->getId()."
"; // get Customer Id echo $customerSession->getCustomer()->getName()."
"; // get Full Name echo $customerSession->getCustomer()->getEmail()."
"; // get Email Name echo $customerSession->getCustomer()->getGroupId()."
"; // get Customer Group Id }

Magento 2 Get Current Date Time


Magento 2 Get Current Date Time– Sometimes we need to get the current date time in Magento 2. It is totally different to get current date time in magento 2 than magento 1. Here in this tutorial, we are going to explain how you can get current Date Time.


Magento 2 Get Current Date Time |Current Store Date Time Example

Magento 2 Get Current Date Time

To use datetime function in magento you need to inject DateTime as \Magento\Framework\Stdlib\DateTime\DateTime, After injecting this you will be able to access the Current Date time functions –

Magento 2 Get Current Date Time Example:

protected $date;
public function __construct(
    \Magento\Framework\Stdlib\DateTime\DateTime $date
) {
    $this->date = $date;
}

public function myFunction(){

$date = $this->date->gmtDate();

}

Thus you can the current date time anywhere using the above method for the current store.

Magento 2 Admin Panel 404 Error page not found


Magento 2 Admin Panel 404 Error page not found : Sometimes we face the issue with magento admin panel. There can be many reason for admin panel 404 error in magento2. Here in this tutorial we are going to explain the possible reason and how to fix it.


Magento 2 Admin Panel 404 Error page not found

You can fix this issue by enabling mod_rewrite.

  • 1. Enable mod_rewrite module and set AllowOverride to the /var/www/html in apache conf.
  • 2. Delete Cache and Access Admin panel

We hope this should work and solve your problem.

Magento2 CSS not Loading


Magento2 Css Not Loading : There are many reasons for css not loading. Here in this tutorial we are going to to explain how you can fix this issue.


Magento2 Css Not Loading Properly Fix It

You can fix the css not loading issue following the below steps –

  • 1. Check permission of /var and /pub folders and set it to 777.
  • 2. Now run the below command –
    php bin/magento2 setup:static-content:deploy
  • 3. Flush cache and check.

Magento2 get current Category


Magento2 get current Category- In magento 2 the way to get the current category has been changed. You can use object manager to get the current category detail in magento2. Here in this tutorial we are going to explain how you can access the current category in magento 2.


Magento2 get current Category

You can get the current category detail in magento 2 using the object manager as below –

Magento2 get current Category :

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    echo $catId = $category->getId();
    //$catName = $category->getName();

The above example will give you the current category details. You can access other details same as we have shown the category id and name.

Magento2 Install Sample Data


Magento2 Install Sample Data – You can use the command magento setup:upgrade and magento sampledata:install. It will install the magento 2 sample data./p>


Magento2 Install Sample Data After Setup

Go to the magento root and run the following command to install the sample data in mangeto2. Here are the steps to install sample data on windows (xampp or wamp).

Step 1

Go to magento root folder and open the composer to the php command –

Magento2 Install Sample Data  using composer

Step 2

Now run the below command –

Magento2 Install Sample Data Steps : Magento 2 Sample Data Example

php bin/magento sampledata:deploy

The command will look something like this –

Magento 2 Sample Data Install

Step 3

Now finalize the sample data installing by running the below command –

Magento2 Install Sample Data Steps : Magento 2 Sample Data Example

php bin/magento setup:upgrade

The command will look something like this –

Magento2 Sample Data Installlation

This will finish the sample data installation in magento2. Now you can use sample data in magento2.

Magento2 PHP Settings Check Error Fix


Magento2 PHP Settings Check Error Fix : While installing the magento 2 checks the Readiness Check which sometimes gives you the above error if it is disabled. If you are facing this problem on server contact your service provider.


Magento2 PHP Settings Check Error Fix

Go to php.ini file and uncomment the below line and set it to ‘-1’ as below –

Magento2 PHP Settings Check Error Fix:

;always_populate_raw_post_data = On
// change this to 
always_populate_raw_post_data = -1

This will solve the above problem. If this is coming on sever please contact your service provider.

Make Sure to restart the apache after making the above settings.