How To Get Magento 2 Store Base URL – The Full Tutorial

by admin

In this article, we will show you how to get your Magento 2 store base URL using very simple code.

Let’s get started!

What Is Magento 2 Store Base URL?

Magento 2 base urls

 

Each Magento website has a base URL assigned to the storefront and another one assigned to the admin.

Magento uses variables to define internal links in relation to the base URL, thus you can move the whole store from one location to another without updating the link.

The standard base URL begins with HTTP, and the secure base URL begins with HTTPS.

  • Base URL: http://www.yourdomain.com/magento/
  • Secure Base URL: https://www.yourdomain.com/magento/
  • URL with IP address: http://###.###.###.###/magento/ or https://###.###.###.###/magento/

How To Get Magento 2 Store Base URL?

There are two methods to get Magento 2 Store Base URL:

  • Using the Magento core method
  • Using object manager

Using the Magento core method

Run the following code:

<?php
namespace [Vendor]\[Module]\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    protected $storeManager;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager
    )
    {
        $this->storeManager = $storeManager;
        parent::__construct($context);
    }

    public function getStoreManagerData()
    {
        $storeUrl = $this->storeManager->getStore()->getBaseUrl();

        // get Store Url without index.php
        $storeUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

        // get Link Url of store
        $storeLinkUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);

        // get media Base Url
        $storeMediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

        // get Static content Url
        $storeStaticUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
    }
}

Using object manager

Run the following code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$storeManager->getStore()->getBaseUrl();  // to get Base Url
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // to get Base Url without index.php
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); // to get store link url
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); // to get Base Media url

The Best Extension For Magento Multiple Store View Pricing

If your eCommerce store has multiple store views, then it is necessary to set different prices for the same product in each of your store views.

For example, in the US store, you would want the product’s price to be displayed in USD, and in the France store, you would want the product’s price to be displayed in EUR.

So today, we want to introduce you to the best solution to manage the product price and currency easily at each store view:

Magento 2 Multiple Store View Pricing by BSS

magento-2-multiple-store-view-pricing

 

This extension allows you to set any price type for a product, including regular price, special price and tier price. And these prices can be different among your store views for the same product.

It also enables you to choose the base currency for each store view and supports checking out with the base currency so your customers can purchase with their preferred currency.

This module allows you to set up Minimum Order Amount, Shipping Cost and Custom Options Price for each store view, too.

Highlight features:

  • Set up different prices, special prices and tier prices for a product per store view 
  • Set up base currency per store view
  • Customers checkout with the base currency of their store view conveniently
  • Set up various special prices for a product per store view 

Conclusion

In this article, we have shown 2 simple methods to get your Magento 2 store base URL.

We hope this blog is helpful and good luck to you!

BSS Commerce is one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bringing high-quality products and services to optimize your business effectively. Furthermore, we offer FREE Installation – FREE 1-year Support and FREE Lifetime Update for every Magento extension.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.