How to Check Website code is valid or not Magento 2?

When you are working for the process of Import product in Magento 2 and You are not aware about website code is exists or not. You can check it from admin panel or programmatic way to check code availability.

You can check the website code from the database table store_website also.

Database Table persist the Website code:
store_website

Optional: You can also create a proxy of StoreManagerInterface in your di.xml file because the interface is resource hungry.

Now, Call the method from the Model file,

$code = “us_website”;
echo $isWebsiteCodeValid = $this->isWebsiteCodeIsValid($code);

Output:
If Website code is valid return website id.
If website code is invalid return null.

Leave a Reply