Chapters Close

How to create multiple websites in Magento 2?

Dec 12th, 2018 4 min to read

This article will help you to understand how one can add multiple websites in Magento 2.

Magento allows you to setup multi-websites on just single installation of Magento. Its configuration is really simple. It will allow you to create a separate website so you can handle the products, customers, product price and many more things on websites bases. 

After installing Magento you need to login into the store and follow the steps given below.

How to create multiple new websites in Magento?

You can create multiple new websites in Magento in 5 steps.

Step 1: Navigate to all store settings and click on create a new website

Navigate to Stores>Settings>All Stores and create a new website, a new store and a new store view. 

This is just the sample data. For instance, we have named our website a German website. After saving this information you will find it in the list as shown below.

Navigate to Stores>Configuration>General>Web>Base Urls, here you need to update the Base Url and Base link URL fields.

Navigate to Stores>Configuration>General>Web>Base Urls (Secure), here you will see that the Base Url and Base link URL fields will be automatically filled.

This image shows the base url section with the required domain name.

Step 3: Configure the store view and select the preferred store view

We need to update these URLs for the german website. You will see a store view option at the top of the page on the left side. Open this drop-down menu and select the german website.

Step 4: Save the configuration

Save this configuration.

Step 5: Open the .htaccess file and add the code

Now we need to open the .htaccess file from our Magento root directory. And find IfModule mod_rewrite.c section. Add below lines of code in this section.

SetEnvIf Host magentogerman.com* MAGE_RUN_CODE=german_website
SetEnvIf Host magentogerman.com* MAGE_RUN_TYPE=website

Now we will be able to access both our websites.

FAQs

Is multiple store creation possible in Magento 2?


Yes, multiple store creation is possible in Magento 2. You can have multiple websites, multiple stores under a single domain, or even multiple domains with a single Magento installation. You can label these additional stores as subdomains and manage them through the admin panel.

What is the difference between a store and a website in Magento 2?


The difference between a store and a website in Magento 2 is that a website is like a parent to the stores. A single Magento website can hold multiple stores, each with their own products and designs but have the same settings and configurations as the parent website. For example, a website can have a default currency and payment method. For every store in the website, the same currency and payment method is applicable, even if the products sold are different.

How do I switch to another website in Magento 2?


To switch to another website in Magento 2, you need to use the admin panel. In the panel, navigate to Stores>Settings>Configuration. In the Store View section, you can see a list of all your websites. Choose the required website from the drop-down menu. Select the “Web” option under the General tab to configure the settings for the selected website.

What is multistore in Magento 2?


Multi store in Magento 2 allows you to create multiple stores with a single Magento installation. Though these stores have different customers, designs, or products, they can be managed by the same admin panel. Multi store is useful in creating different versions of the same store by changing the currency and language.

How to create and use language-pack component in Magento 2?

Oct 29th, 2018 4 min to read

If you are new to create a custom component in Magento 2 and if you are in the requirement to have a working language component, you landed up at the right place.

In Magento 2, each component type has a different directory structure. To create a language-package component, we have to create a vendor directory and package directory under the following directory: app > i18n

Follow the below steps to create a language-package component:

  • Step 1: Create a directory for the language package
  • Step 2: Collect Phrases for translation
  • Step 3: Translate word or phrases
  • Step 4: Create registration.php, language.xml and composer.json file in the language package directory
  • Step 5: Flush the Magento cache and static file cache

Note: Replace Aureatelabs/it_ch from entries in this article to your module name.

Step 1: Create a directory for the language package

The directory structure for the language package will look like this: <root>/app/i18n/<Vendor>/<Package>

For example: <root> > app > i18n > Aureatelabs > it_ch

Step 2: Collect Phrases for translation

Generate a .csv file, using the command given below. But instead of specifying a module or theme directory for CSV generation, specify the Magento application root directory. Generated .csv file will contain any phrases that the command will find in the code.

bin/magento i18n:collect-phrases -o “/app/i18n/Aureatelabs/it_ch/it_CH.csv” -m

Step 3: Translate words and phrases

Use the following instruction for translating the words and phrases:

  • Change the content of the second column only. Translate the phrases from English to the respective language.
  • While translating, pay attention to placeholders like %1, %2 and so on. They are used by the Magento application to insert context values, they are not used for translations. For example:
    • Product ‘%1’ has been added to the shopping cart.
    • Product ‘Multimeter-2000’ has been added to the shopping cart.

Step 4: Create registration.php, language.xml and composer.json file in the language package directory

registration.php
Create a registration.php file on language package root directory for registering the component with the following code.

\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
   'aureatelabs_it_ch',
   __DIR__
);

language.xml
Create a language.php file on language package root directory with the following code.

<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
   <code>it_CH</code>
   <vendor>aureatelabs</vendor>
   <package>it_ch</package>
</language>

Where:

  • <code>: Language package locale (required)
  • <vendor>: Module’s vendor name (required)
  • <package>: Language package name (required)
  • <sort_order>: Priority of uploading a package when there are several language packages available for a store (optional)
  • <use>: Parent language package locale from which to inherit dictionaries (optional)

composer.json
Create a composer.json file on language package root directory with the following code.

{
 "name": "aureatelabs/language-it_ch",
 "description": "Italian language",
 "version": "100.0.2",
 "license": [
   "OSL-3.0",
   "AFL-3.0"
 ],
 "require": {
   "magento/framework": "100.0.*"
 },
 "type": "magento2-language",
 "autoload": {
   "files": [
     "registration.php"
   ]
 }
}

Step 5: Flush the Magento cache and static file cache

Now, run the following commands from the command line interface
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento cache:flush

That is all you need to set up and you are all good to go with the newly created language-package component. Though you faced any difficulties, queries even after following all the steps carefully, connect us anytime and we will be glad about solving your issues.

Happy Translating!

We discussed how to set custom prices programmatically in the last post. Now, in this article, we will be guiding you on one of the most frequently asked queries related to Magento web development. That is, how to convert the price from one currency to another in the magento 2.

The first thing you need to do is to create an instance of “\Magento\Directory\Model\CurrencyFactory” and “\Magento\Store\Model\StoreManagerInterface” as given below:

/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
public $currencyFactory;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
public $storeManager;

/**
* Data constructor.
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
   \Magento\Framework\App\Helper\Context $context,
   \Magento\Directory\Model\CurrencyFactory $currencyFactory,
   \Magento\Store\Model\StoreManagerInterface $storeManager
) {
   $this->currencyFactory = $currencyFactory;
   $this->storeManager = $storeManager;
   parent::__construct($context);
}

Then you need to use the function given below to get the price converted from one currency to another currency.

public function convertPrice($price, $currencyCodeFrom, $currencyCodeTo)
{
   $rate = $this->currencyFactory->create()
                ->load($currencyCodeFrom)
                ->getAnyRate($currencyCodeTo);
   
   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

In the snippet above,
$price : amount that you want to convert
$currencyCodeFrom : currency code from which the price has to be converted
$currencyCodeTo : currency code in which you want the price to be converted

Convert price from current currency to base currency

You need to use the following function to get the converted price from current currency to base currency.

public function convertPriceFromCurrentCurrencyToBaseCurrency($price)
{
   $curentCurrencyCode =  $this->storeManager->getStore()
                               ->getCurrentCurrency()
                               ->getCode();
   $baseCurrencyCode =  $this->storeManager->getStore()
                             ->getBaseCurrency()
                             ->getCode();

   $rate = $this->currencyFactory->create()
                 ->load($curentCurrencyCode)
                 ->getAnyRate($baseCurrencyCode);

   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

Convert price from current currency to another currency

You need to use the following function to get the converted price from current currency to another currency.

public function convertPriceFromCurrentToAnotherCurrency($price, $currencyCodeTo)
{
   $curentCurrencyCode =  $this->storeManager->getStore()->getCurrentCurrency()->getCode();
   $rate = $this->currencyFactory->create()
                ->load($curentCurrencyCode)
                ->getAnyRate($currencyCodeTo);

   $convertedPrice = $price * $rate;

   return $convertedPrice;
}

In this example, I have implemented this code in the helper, but you can also implement this in a Block or a Controller as per your requirement.

Also read: How to add a column to tier pricing in Magento 2

Grow your online business like 2,369 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.