menu

Author Archives: admin

Magento 2 get Customer Details


Magento 2 get Customer Details– It is totally different to get the customer details in Magento 2. You can use object manager or service contracts(\Magento\Customer\Api\CustomerRepositoryInterface). Here in this tutorial we are going to explain how to get customer details using customer id or customer session. We will cover the other details like -email, name, shipping address, billing address details etc.


Magento 2 get Customer Details Example

First of all, we need to get the customer object either by session or load customer by id. Let us go one by one-

Load Customer Data By Id

You can use object manager to load the customer by id simply as below –

Magento 2 get Customer Details Example:

Get Customer Data From Session

If customer is logged in you can get the customer object from session using object manager simply as below-

Magento 2 Get Customer Session Example:| Get current customer Id

Using the above method you can check whether customer is logged in or not. Now let us get more details about the customer using the above customer object.

  • Customer Id
  • Customer Name(FirstName, LastName)
  • Customer Email Id
  • Customer Group Id

Get Customer Id | First Name | Last Name | Email Id | Customer Group Id

Get Customer Data in Magento 2 Example:

The above example will give you the customer details.


Get Customer Address Details


  • Default Shipping Address Details
  • Default Billing Address Details

Get Shipping Address Details | By Default Address Id

Customer’s Default Shipping Address Can Be Loaded Using the Default Address Id. You can get the following shipping details from Shipping Address Object-

  • Shipping FirstName
  • Shipping LastName
  • Shipping Company
  • Shipping Zip Code
  • Shipping City
  • Shipping Street
  • Shipping Telephone
  • Shipping Country

Load Customer Address By Address Id Example:

Get Billing Address Details | By Default Address Id

Customer’s Default Billing Address Can Be Loaded Using the Default Address Id. You can get the following shipping details from Billing Address Object-

  • Billing FirstName
  • Billing LastName
  • Billing Company
  • Billing Zip Code
  • Billing City
  • Billing Street
  • Billing Telephone
  • Billing Country

Load Customer Billing Address By Billing Id Example:

Magento 2 Get Shopping Cart Details


Magento 2 Get Shopping Cart Details- – In magento 2 we can use object manager to get the shopping cart details. It is a little bit different than the magento 1. Here in this tutorial we are going to explain how you can use object manager to get the Shopping cart details such as – Cart totals, total items, items details etc.


Magento 2 Get Shopping Cart Details | Items | Quantity Example

First you need to create the instance of object manager, then you need to create object for Magento\Checkout\Model\Cart. Here is an example –

Magento 2 Get Shopping Cart Details, Items Example:

In the above example we have used object manager to get the cart items list. We have fetched the cart item name, Item Quantity and product image, on the same way you can get the other details related to the customer’s cart.

Magento 2 Display Category Products on Home Page


Magento 2 Display Category Products on Home Page– Sometimes we need to display some specific category products on homepage. It is a little bit tricky to display the category products on Home Page. Here in this tutorial, we are going to explain how you can display the products from selected category on home page.


Magento 2 Display Category Products on Home Page Example

You can display the catgory products on Home page by using the following code-

Magento 2 Display Category Products on Home Page Example:

In the above example 14 is the category id for which we need to get the list of products.

So replace 14 by your category id while using the above code. We hope the above example will help you and save your time.

Turn On Error Messages In Magento 2


How to turn on Error Messages In Magento 2 – Error Reporting is very importtant for any framework, it helps us to track the errors during development. Here in this article we are going to explain how you can enable error messages in Magento 2.


Turn On Error Messages In Magento 2 | Enable Error Reporting

It is a little bit different to enable error reporting in magento.
.There are following methods to enables errors.

Method 1 : Set Developer Mode

To enable the error messages to show on frontend, backend you need to set the Developer Mode. Here is the following command to set the developer mode –

Magento 2 Enable Error Reporting | Set Developer Mode Example:

Now you will be able to see the errors occurred.

Method 2: Using Traditional Method

Alternatively you can add the following lines of code to the index.php to enable the erros & warnings.

Enable Erros & Warning Using Index.php Example:

Magento 2 Modes

Primarily Magento 2 provides the following modes which can be used easily for various stages of a project. Let us look at the modes available-

  • Default Mode– When you install fresh magento 2 it sets the default mode it means error will not be displayed on front end but errors can be seen in Magento error log.
  • Developer Mode– If you enable the develpoer mode frontend errors will also be displayed.
  • Production Mode– If you set the production mode then the errors and warnings will not be generated.

Show Current Mode

If you want to see the current mode run the following command-

Magento 2 Show Current Mode CLI Example:

The above command shows the current mode instance.

Magento 2 Command Line List


Magento 2 Command Line List– As we all know magento 2 comes with some predefined commands which enable us to use it easily. Using these commands we can perform an important task such as – clearing cache, enable/disable developer mode, reindexing etc. Here in this tutorial we are going to cover the important commands which are used in daily development life.


Magento 2 Command Line List | Useful Commands

There are following commands available in magento 2 –

  • Setup Commands

  • Setup Upgrade Command Line

    Setup upgrade command is very simple you can upgrade the setup using the below command-

    Upgrade Setup Command Line Example:

  • Cache Commands

  • Cache Enable Command Line

    You can enable cache using the below command –

    Cache Enable Command Line Example:

  • Cache Disable Command Line

    You can disable cache using the following command –

    Cache Disable Command Line Example:

  • Cache Clean Command Line

    Following command is used to clean cache in Magento-

    Cache Clean Command Line Example:

  • Cache Flush Command Line

    Cache Flush Command Line Example:

  • Cache Status Command Line

    You can use the following command to check the cache status-

    Cache Status Command Line Example:

  • Indexer Commands

  • Indexer Status Command Line

    Following command is used to get the indexer status-

    Indexer Status Command Line Example:

  • Indexer Show Mode Command Line

    Indexer Show Mode Command Line Example:

  • Indexer Reindex Command Line

    Indexer Reindex Command Line Example:

  • Indexer Info Command Line

    Indexer Info Command Line Example:

  • Module Commands

  • Enable Module Command Line

    In magento 2 You can enable module using command line simply as below-

    Enable Module Using Command Line Example:

  • Disable Module Command Line

    You can disable module using command line simply as below-

    Disable Module Using Command Line Example:

  • Uninstall Module Command Line

    In magento 2 You can uninstall module using command line simply as below-

    Uninstall Module Using Command Line Example:

  • Other Commands

  • Check Current Mode Command Line

    You can check the current mode(Production/Development) using command line simply as below-

    Check Mode(Development/Production) Module Using Command Line Example:

  • Set Developer Mode Command Line

    You can set the developer mode simply as below-

    Enable Developer Mode Module Using Command Line Example:

  • Set Production Mode Command Line

    You can set the production mode simply as below-

    Set Production Mode Module Using Command Line Example:

  • Run Single Tenant Compiler Command Line

    Run Single Tenant Compiler Command Line Example:

  • Set Static Content Deploy Command Line

    Set Static Content Deploy Command Line Example:

  • Set Static Content Deploy Command Line For Language

    Set Static Content Deploy Command Line for language Example:

  • Set Static Content Deploy Command Line FOr Theme

    Set Static Content Deploy For theme Command Line Example:

  • Install Sample Data

    Here is simple command to install the sample data in Magento.

    Install Sample Data Command Line Example:

  • Run Cron

    Run Cron Command Line Example:

Magento 2 Reindex Command Line


Magento 2 Reindex Command Line– It is very easy and simple to reindex the data in magento 2, you can perform reindexing using the command line. Here in this tutorial we are going to explain how you can reindex the data using command line in magento 2.


Magento 2 Reindex Command Line Example

First go to your magento root directory then run the below command –

Magento 2 Reindex Command Line Syntax | Example:

This will perform the full indexing. If you to run some specific index then you can run it simply as below-

Magento 2 Get Indexer Name

You can get the indexer name in magento 2 simply as below-

Magento 2 Get Indexer Info Example:

If you run the above it will give you the list of inxdexer names.

Magento 2 Run Specific Indexer By Name

You can run any specific indexer by name simply as below-

Magento 2 Run Specific Indexer | Example:

So the above command will run the specific indexer by name. [indexer_name] is name of indexer.

Magento 2: reindexing is not working?

If your reindexing command is not working please make sure you are running the above commands in your magento 2 project’s root directory.

Bootstrap Change Default Background Color

Bootstrap Change Default Background Color Css– It is very simple to change the default background color in bootstrap. There are many ways to change the default background color in bootstrap, here in this tutorial we are going to change the default background color using CSS.


Bootstrap Change Default Background Color | Css | Example

Here in this article we are going to change body color by add the custom color to body Simply as below-

Bootstrap Change Default Background Color Example:

Using the above method you can override the default background color. You need to add !important to override the bootstrap’s default style. If you don’t want to add the background color uisng the default body tag, you can assign some class to body then add the same style as above.

Bootstrap Add Background Image In Navbar

Bootstrap Add Background Image In Navbar– Sometimes we need to add background image to the navigation bar in bootstrap. There are many ways to do this, here in this tutorial we are going to add the background image with the help of existing class and adding some custom style to it.


Bootstrap Add Background Image In Navbar Example

You can add the background image to the navbar using the class .navbar-inner Simply as below-

Bootstrap Add Background Image In Navbar Example:

On the same way you can override the other navbar styles. If you don’t want to add the background image to the core bootstrap class .navbar-inner, you can create your own custom class and add the above style.

Magento 2 Create Event Observer


Magento 2 Create Event Observer– Observers are great ways to extend the default functionality in Magento. Using Observers we can easily implement the custom changes required related to different-different functionalities. Here in this tutorial we are going to explain how you can create Event Observers in Magento 2.


Magento 2 Create Event Observer Example

Creating observer event in Magento 2 is totally different than the Magento 1. We need to create events.xml configuration file to create events-

Step 1 – Create events.xml

Go to etc folder and create events.xml and define the event obsever simply as below-

Magento 2 Create Event Observer Example:

Step 2 – Create Obsever

Now go to Observer folder and create folder named as Sales. Inside this sales folder create OrderPlaceAfter.php and add the code as below-

Magento 2 sales order place after Event Example:

Thus you can create any observer event in magento 2.

Download Sample Code

If you want to download the sample code you can download it from here – Magento 2 Observer Example Sample