Magento 2 Get Store Base URL

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

by admin

In this article, we’ll guide you through the process with a straightforward piece of code, making it accessible even for those with minimal coding experience. Whether you’re a seasoned developer or a Magento newbie, this tutorial on Magento 2 get store base URL will help you effortlessly get your store’s base URL and ensure smooth operations for your online business. Let’s dive in and simplify your Magento 2 management! 

Let’s get started!

What Is Magento 2 Store Base URL?

Magento 2 base urls settings

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/

>>> READ MORE: [2024 Update] Magento 2 Tutorial for Developers & Merchants

Magento 2 Get Store Base URL: Step-by-Step

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

  • Using the Magento core method
  • Using object manager

Magento 2 Get Base URL with Store Code

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);
    }
}

Get Store Base URL Magento 2 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 summary, there are two primary methods to master the “Magento 2 get store base URL” process: using the Magento core method and employing the object manager. Both approaches are effective and can be chosen based on your specific needs and preferences.

Additionally, if you are managing multiple stores, consider Magento 2 Multiple Store View Pricing Extension by BSS Commerce. With this Magento module, you can effortlessly configure your Magento 2 store and set up different prices for a product for different store views.

 

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.