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

app/code/Metagento/StockQty/view/frontend/layout/catalog_product_view.xml

 

Block file

app/code/Metagento/StockQty/Block/Catalog/Product/View.php

 

Template file

app/code/Metagento/StockQty/view/frontend/templates/catalog/product/view.phtml

 

Route file

app/code/Metagento/StockQty/etc/frontend/routes.xml

 

Controller 

app/code/Metagento/StockQty/Controller/Index/Index.php

 

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

Result

  • 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: https://github.com/metagento/magento-2-stock-qty

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.