Magento 2 – Get Base URL and Current URL

In this post, we will show you how to get base url and current url in Magento 2. 

If you want to get Base url:

Where $storeManager is instance of \Magento\Store\Model\StoreManagerInterface

You also can use ObjectManager if $storeManager is not available

For Getting Media Url Use Below code:

Get Static content Url

Get Current URL

However, using object manager is not a recommend way. You should init $storeMaanger variable in __construct() method

Then use following code to get URLs

This is the guide to get base URL and other URLs in Magento 2. If that you have any queries about the article or any questions in general, use the comment section below or contact us

Magento 2 Admin Session Lifetime

In Magento by default, the Admin is initially set to time out after 900 seconds, 15 minutes of inactivity.

This may cause a problem that you have to login many times. In this tutorial, we will show you how to adjust the lifetime of the admin session to fit your work style.

What we have to do:

  • Change setting in backend
  • Check and edit PHP environment variables

1. Change session life time in backend configuration

On the Admin sidebar, choose Stores -> Settings -> Configuration

In the panel on the left under Advanced, choose Admin

Expand Security  then go to field Admin Session Lifetime (seconds)

Set the new lifetime in second, example 1 day = 86400 then click on save configuration

Magento 2 Admin session by Metagento

2. Check/Edit PHP environment variables

After setting the new lifetime in Magento backend, we need to make sure that your server allows to use this value.

Create php.ini file in your Magento root directory ( if it already exist, skip this step )

Add following lines to the file then save it:


php_value session.cookie_lifetime 86400

php_value session.gc_maxlifetime 86400


3. Clear Magento cache, logout current session and login again


That’s how to change the admin session lifetime on Magento. You can use another value to make it work as you want.

If you have any question or need assistance, leave us a message.

Magento 2 Show Stock Quantity on Product Page

By default, Magento only shows stock status. In this tutorial, we will show you how to show stock quantity on product page on Magento 2.

You can get the final package for this feature at the end of this post. 

List of files we will create:

  • app/code/Metagento/StockQty/view/frontend/layout/catalog_product_view.xml
  • app/code/Metagento/StockQty/Block/Catalog/Product/View.php
  • app/code/Metagento/StockQty/view/frontend/templates/catalog/product/view.phtml
  • app/code/Metagento/StockQty/etc/frontend/routes.xml
  • app/code/Metagento/StockQty/Controller/Index/Index.php

Layout file



Block file



Template file



Route file






After creating above files, clear magento cache to see the change.


  • Simple product page

Show Stock Quantity on Simple Product Page

  • Configurable product page

Show Stock Quantity on Configurable Product Page

That’s how to show stock quantity on product page, hope this small feature will be helpful.

You can get this module on Github:

If you have any question or need any assistance, leave us a message

Magento 2 – Add Product to Cart Programmatically

While building an extension, you may want a function which add product to cart in background.
Example, give customer free gifts.

In this tutorial, we will create a method to programmatically add product to cart in Magento 2. 
Use following code to create a controller to test this function

Hope this tutorial is helpful. If you have any question, just leave us a message.
Thank you!

Magento 2 – Set custom page title for Category Pages

Set page title for Category Page in Magento 2 – By default, Magento uses the category name for page title. However, sometimes we want to set the page title manually.

In this tutorial, we will tell you a way to change category page title without editing the category name.
We will use layout update feature.

1. Go to the category you want to edit title

Admin Menu -> Catalog -> Categories -> Select the category -> Choose store scope ( optional )

2. Select tab “Design” and field “Layout Update XML”

Set page title for category pages

3. Enter following code

4. Save the category and clear cache.

That’s how we use a custom page title for category page.
Thanks for reading! Leave us a message or contact us if you need assistance.

Magento 2 Create Customer Programmatically

In Magento 2, there are some ways to create a new customer, fill the form in register page, create customer in backend, checkout … However, if you want to create account in some automation process, you will have to do it with code. This tutorial will show you how to create customer programmatically in Magento 2

Code snippet to create customer account

Code snippet to set address for customer account

Hope above code can help you save a little time on coding. In case you want to add many customers, you can apply the part of the code in a loop

Magento 2 Disable Newsletter Subscription Success Email

In Magento 2, you’re able to enable/disable email for newsletter confirmation at Store -> Configuration -> Customers -> Newsletter. However, there is no setting to disable newsletter success email. In this tutorial, we will tell you how to do that.

You can get the package of this module in the end of article.

Disable newsletter subscription success email - Metagento

Where does Magento 2 send the subscription success email?

The sending email process can be found in file:

/vendor/magento/module-newsletter/Model/Subscriber.php method sendConfirmationSuccessEmail 

Editing Magento core file is a bad option, your edited code will be removed when you upgrade Magento. So we will create a simple module to do that.

In this post, we just tell you how to disable subscription success email. You can read this post to know how to create an extension on Magento 2

Assuming that we already have an extension named “Metagento_DisableNewsletterSuccess“, let’s find out what to do next.

1. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/di.xml

2. Create new file: /app/code/Metagento/DisableNewsletterSuccess/etc/adminhtml/system.xml

This part will create a new setting, where you can enable/disable this function in the future.

3. Create new file: /app/code/Metagento/DisableNewsletterSuccess/Plugin/Newsletter/Model/Subscriber.php

In this file, we will check the setting first. If the function is enabled, we set the import mode to stop sending emails.

4. After creating all files, execute following commands to install the module.

5. Check again the configuration and enable the function

Disable newsletter subscription email by Metagento

You can get the package of this module on github here:

We also provide a free extension which gives subscriber discount coupon codes.


Magento 2 Permission for Files and Directories

In Magento 2 permission is very important. Some features ( or whole website ) won’t work properly if you don’t set the permission correctly. So what are the correct file and folder permissions for Magento 2 and How to set Magento 2 file/folder permissions? Let’s find out

Magento recommend setting the permissions of file/folder in Magento 2 as below:

  • 755 for directories: User has a full control and Group/World can traverse directories;
  • 644 for files: User – read, write; Group/ World – read.


It’s clear but how to set the permission like that? We can set them using following commands ( via SSH )

You may need to clear Magento cache then, using command

That’s all we need to do to set correct permission for files and directories in Magento 2

If you have any question, just leave us a message.

Magento 2 clear cache programmatically

In this post, let’s find out how to clear/flush cache programmatically in Magento 2. It will be useful for you in your custom module or new feature. 

Whenever you make any modifications to your Magento website, you will need to flush cache to see the changes. And as you may know, there are some ways to do that.

  1. Go to System -> Tools -> Cache Management then select Flush Magento Cache
  2. Remove var/cache directory using File Manager in cPanel, FTP or SSH
  3. Using code ( helpful for automation ) 


In Magento 1


In Magento 2

  • Define constructor

  • Create a function to flush magento cache

You can edit $types variable if you don’t want to clear all cache types.


Using above code, we can clear Magento cache instead of using magento admin panel or removing var/cache folder. If you need any assitance, leave us a message

Magento 2 component manager please wait stop loading

Magento 2 component manager please wait problem 

In Magento 2 backend, when you try to upgrade modules or Magento using Component Manager, you may get this problem: Magento keeps showing ‘Please wait’ message and it won’t stop.

Magento 2 component manager please wait

There are many reasons why you get this issue. However, in our case, it came from custom module composer and here is how we get it solved.

Go to this file:  /setup/src/Magento/Setup/Model/PackagesData.php

Create a backup of this file before editing.

Then replace it using this file:


You can read more discussion about this issue here:

And this is where you can get this file from Magento: PackageData.php from Magento

That’s how we solve the issue when Magento 2 component manager please wait stop loading. Hope it will be helpful.