cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - How to create website , Store , Store view , Root Category Programmatically

SOLVED

Magento 2 - How to create website , Store , Store view , Root Category Programmatically

How to create Website , Store , Store View , Store Specific Root Category Programmatically ?

 

I Have Tried below code : 

 

/** @var \Magento\Store\Model\Website $website */
        $website = $this->websiteFactory->create();
        $website->load('my_custom_code');
        if(!$website->getId()){
            $website->setCode('my_custom_code');
            $website->setName('My Custom Name');
            $website->setDefaultGroupId(3);
            $this->websiteResourceModel->save($website);
        }
        
        if($website->getId()){
            /** @var \Magento\Store\Model\Group $group */
            $group = $this->groupFactory->create();
            $group->setWebsiteId($website->getWebsiteId());
            $group->setName('My Custom Group Name');
            $group->setRootCategoryId(2);
            $group->setDefaultStoreId(3);
            $this->groupResourceModel->save($group);
        }
        
        /** @var  \Magento\Store\Model\Store $store */
        $store = $this->storeFactory->create();
        $store->load('my_custom_store_code');
        if(!$store->getId()){
            $group = $this->groupFactory->create();
            $group->load('My Custom Group Name', 'name');
            $store->setCode('my_custom_store_code');
            $store->setName('Mu Custom Store Code');
            $store->setWebsite($website);
            $store->setGroupId($group->getId());
            $store->setData('is_active','1');
            $this->storeResourceModel->save($store);
            // Trigger event to insert some data to the sales_sequence_meta table (fix bug place order in checkout)
            $this->eventManager->dispatch('store_add', ['store' => $store]);
        }    

But few options are still not working , like store code is not generating, store specific category code is not there etc.

 

 

So whats the best approach to create all 4 website ,store view,store,store specific category programatically. ?

 

I have already refer this link as well - https://gist.github.com/0-Sony/1297fd13e11e9b901fa403a618b6cee5

 

Advanced Thanks ,Your help will be appriciated.

 

if issue solved,Click Kudos & Accept as Solution
Tags (1)
3 REPLIES

Re: Magento 2 - How to create website , Store , Store view , Root Category Programmatically

Hi @ManthanDave,

 

I didn't tested but did you check this thread: https://magento.stackexchange.com/questions/119907/how-to-create-store-view-or-store-programmaticall... ?

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: Magento 2 - How to create website , Store , Store view , Root Category Programmatically

Yes i have already seen that thread.

 

But as per my requirement i needed some better way as i am creating store dynamically with my custom grid !!

 

so looking for an alternative way to do this !!

 

if issue solved,Click Kudos & Accept as Solution

Re: Magento 2 - How to create website , Store , Store view , Root Category Programmatically

So as per my need , i have created this dynamically , i have created my separate form with grid and getting  field values from there and while clicking on save button it will generates all 3 dynamically.

 

Below is the code for Website Creation : 

 

 

public function getWebsite()
    {
        $group = $this->groupFactory->create();

        if($this->getGroupId())
        {
            $group->load($this->getGroupId());

            if($group->getId() && $group->getWebsiteId())
            {
                $website = $this->websiteFactory->create();
                $website->load($group->getWebsiteId());
                return $website;
            }
        }

        return null;
    }

 

Below is the code for store creation : 

 

private function createOrUpdateGroup($groupId, $websiteId, $storeName, $storeCode)
    {
        $group = $this->groupFactory->create();
        if($groupId)
            $group->load($groupId);

        $group->setWebsiteId($websiteId);
        $group->setName($storeName);
        $group->setCode($storeCode);
        $group->save();

        return $group;
    }

 

Below is the code for Root Category :

 

private function createOrUpdateRootCategory($data, $categoryId = 0)
    {
        $rootCategoryName = $data['name'];

        $category = $this->categoryFactory->create();

        if($categoryId != 0)
            $category->load($categoryId);

        $category->setName($rootCategoryName . ' - Default Category');
        $category->setIsActive($data['is_active']);
        $category->setStoreId(0);

        if($categoryId == 0)
        {
            $parentCategory = $this->categoryFactory->create();
            $parentCategory->load(\Magento\Catalog\Model\Category::TREE_ROOT_ID);

            $category->setDisplayMode(\Magento\Catalog\Model\Category::DM_PRODUCT);
            $category->setPath($parentCategory->getPath());
        }

        $category->save();

        return $category->getId();
    }

Below is the code for store view : 

 

private function createOrUpdateStore($group, $websiteId, $storeViewName, $storeViewCode, $data)
    {
        $store = $this->storeFactory->create();
        if($group->getId() && $group->getDefaultStoreId())
            $store->load($group->getDefaultStoreId());

        $store->setName($storeViewName . ' - Store View');
        $store->setCode($storeViewCode);
        $store->setWebsiteId($websiteId);
        $store->setGroupId($group->getId());
        $store->setSortOrder($data['sort_order']);
        $store->setIsActive($data['is_active']);
        $store->save();

        return $store;
    }

 

 

 

 

 

 

 

if issue solved,Click Kudos & Accept as Solution