Creating website programmaticallydata: current version - none, required version - 2.0.0Setup version for module 'Sample_CustomController' is not specifiedI created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Unable to synchronized app import configuration failedNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Trying to uninstall moduleMagento 2 Front controller reached 100 router match iterations issueDeleted ShipperHQ module causing error in “All Customers” section of Magento 2“Area code is not set” in var/logwhen click on place order then paypal showing error in Magento2

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

How to draw a matrix with arrows in limited space

Why do Radio Buttons not fill the entire outer circle?

How to make money from a browser who sees 5 seconds into the future of any web page?

Why should universal income be universal?

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

C++ check if statement can be evaluated constexpr

What (the heck) is a Super Worm Equinox Moon?

15% tax on $7.5k earnings. Is that right?

Will number of steps recorded on FitBit/any fitness tracker add up distance in PokemonGo?

Does the Linux kernel need a file system to run?

Quoting Keynes in a lecture

Can I cause damage to electrical appliances by unplugging them when they are turned on?

awk assign to multiple variables at once

Pre-mixing cryogenic fuels and using only one fuel tank

Multiplicative persistence

Doesn't the system of the Supreme Court oppose justice?

How many arrows is an archer expected to fire by the end of the Tyranny of Dragons pair of adventures?

What does Apple's new App Store requirement mean

What is the difference between lands and mana?

I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

What is Cash Advance APR?

Did the UK lift the requirement for registering SIM cards?



Creating website programmatically


data: current version - none, required version - 2.0.0Setup version for module 'Sample_CustomController' is not specifiedI created a custom module ,but getting error, not able to figure out what the error is about. How to get out of this error?Unable to synchronized app import configuration failedNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Trying to uninstall moduleMagento 2 Front controller reached 100 router match iterations issueDeleted ShipperHQ module causing error in “All Customers” section of Magento 2“Area code is not set” in var/logwhen click on place order then paypal showing error in Magento2













1















I'm using magento 2.3. I'm trying to create a website through a custom command. Here's my code



$website = $this->websiteFactory->create();
$website->setName("foo")->setCode("foobar");
$website->save();
var_dump("expression");


This gives an error - Validation Failed. In Save.php line 59.



However, it does create the website.



If I remove $website->save(), "expression" is displayed.



Stack trace:



Exception #0 (MagentoFrameworkValidationValidationException): Validation Failed
#0 vendor/magento/module-inventory/Model/StockRepository.php(67): MagentoInventoryModelStockCommandSave->execute(Object(MagentoInventoryModelStock))
#1 vendor/magento/framework/Interception/Interceptor.php(58): MagentoInventoryModelStockRepository->save(Object(MagentoInventoryModelStock))
#2 vendor/magento/framework/Interception/Interceptor.php(138): MagentoInventoryModelStockRepositoryInterceptor->___callParent('save', Array)
#3 vendor/magento/framework/Interception/Interceptor.php(153): MagentoInventoryModelStockRepositoryInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoInventoryModelStock))
#4 generated/code/Magento/Inventory/Model/StockRepository/Interceptor.php(26): MagentoInventoryModelStockRepositoryInterceptor->___callPlugins('save', Array, Array)
#5 vendor/magento/module-inventory-sales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php(100): MagentoInventoryModelStockRepositoryInterceptor->save(Object(MagentoInventoryModelStock))
#6 vendor/magento/framework/Interception/Interceptor.php(146): MagentoInventorySalesPluginStoreModelResourceModelWebsiteAssignWebsiteToDefaultStockPlugin->afterSave(Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelWebsiteInterceptor))
#7 vendor/magento/framework/Interception/Interceptor.php(153): MagentoStoreModelResourceModelWebsiteInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoStoreModelWebsiteInterceptor))
#8 generated/code/Magento/Store/Model/ResourceModel/Website/Interceptor.php(130): MagentoStoreModelResourceModelWebsiteInterceptor->___callPlugins('save', Array, NULL)
#9 app/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index.php(27): MagentoStoreModelResourceModelWebsiteInterceptor->save(Object(MagentoStoreModelWebsiteInterceptor))
#10 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(24): SampleModuleControllerAdminhtmlHelloWorldIndex->execute()
#11 vendor/magento/framework/App/Action/Action.php(108): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->execute()
#12 vendor/magento/module-backend/App/AbstractAction.php(231): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#13 vendor/magento/framework/Interception/Interceptor.php(58): MagentoBackendAppAbstractAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#14 vendor/magento/framework/Interception/Interceptor.php(138): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callParent('dispatch', Array)
#15 vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#16 vendor/magento/framework/Interception/Interceptor.php(135): MagentoBackendAppActionPluginAuthentication->aroundDispatch(Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#17 vendor/magento/framework/Interception/Interceptor.php(153): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#18 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(39): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callPlugins('dispatch', Array, NULL)
#19 vendor/magento/framework/App/FrontController.php(159): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#20 vendor/magento/framework/App/FrontController.php(99): MagentoFrameworkAppFrontController->processRequest(Object(MagentoFrameworkAppRequestHttp), Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor))
#21 vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
#22 vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
#23 vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#24 generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, Array)
#25 vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#26 generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
#27 vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttpInterceptor->launch()
#28 pub/index.php(37): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
#29 main


What am I doing wrong? What is the correct way to create a website programmatically?










share|improve this question
























  • try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

    – Marius
    Feb 26 at 18:00











  • Still getting the same error

    – coderGeek
    Feb 26 at 18:02















1















I'm using magento 2.3. I'm trying to create a website through a custom command. Here's my code



$website = $this->websiteFactory->create();
$website->setName("foo")->setCode("foobar");
$website->save();
var_dump("expression");


This gives an error - Validation Failed. In Save.php line 59.



However, it does create the website.



If I remove $website->save(), "expression" is displayed.



Stack trace:



Exception #0 (MagentoFrameworkValidationValidationException): Validation Failed
#0 vendor/magento/module-inventory/Model/StockRepository.php(67): MagentoInventoryModelStockCommandSave->execute(Object(MagentoInventoryModelStock))
#1 vendor/magento/framework/Interception/Interceptor.php(58): MagentoInventoryModelStockRepository->save(Object(MagentoInventoryModelStock))
#2 vendor/magento/framework/Interception/Interceptor.php(138): MagentoInventoryModelStockRepositoryInterceptor->___callParent('save', Array)
#3 vendor/magento/framework/Interception/Interceptor.php(153): MagentoInventoryModelStockRepositoryInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoInventoryModelStock))
#4 generated/code/Magento/Inventory/Model/StockRepository/Interceptor.php(26): MagentoInventoryModelStockRepositoryInterceptor->___callPlugins('save', Array, Array)
#5 vendor/magento/module-inventory-sales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php(100): MagentoInventoryModelStockRepositoryInterceptor->save(Object(MagentoInventoryModelStock))
#6 vendor/magento/framework/Interception/Interceptor.php(146): MagentoInventorySalesPluginStoreModelResourceModelWebsiteAssignWebsiteToDefaultStockPlugin->afterSave(Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelWebsiteInterceptor))
#7 vendor/magento/framework/Interception/Interceptor.php(153): MagentoStoreModelResourceModelWebsiteInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoStoreModelWebsiteInterceptor))
#8 generated/code/Magento/Store/Model/ResourceModel/Website/Interceptor.php(130): MagentoStoreModelResourceModelWebsiteInterceptor->___callPlugins('save', Array, NULL)
#9 app/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index.php(27): MagentoStoreModelResourceModelWebsiteInterceptor->save(Object(MagentoStoreModelWebsiteInterceptor))
#10 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(24): SampleModuleControllerAdminhtmlHelloWorldIndex->execute()
#11 vendor/magento/framework/App/Action/Action.php(108): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->execute()
#12 vendor/magento/module-backend/App/AbstractAction.php(231): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#13 vendor/magento/framework/Interception/Interceptor.php(58): MagentoBackendAppAbstractAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#14 vendor/magento/framework/Interception/Interceptor.php(138): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callParent('dispatch', Array)
#15 vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#16 vendor/magento/framework/Interception/Interceptor.php(135): MagentoBackendAppActionPluginAuthentication->aroundDispatch(Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#17 vendor/magento/framework/Interception/Interceptor.php(153): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#18 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(39): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callPlugins('dispatch', Array, NULL)
#19 vendor/magento/framework/App/FrontController.php(159): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#20 vendor/magento/framework/App/FrontController.php(99): MagentoFrameworkAppFrontController->processRequest(Object(MagentoFrameworkAppRequestHttp), Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor))
#21 vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
#22 vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
#23 vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#24 generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, Array)
#25 vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#26 generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
#27 vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttpInterceptor->launch()
#28 pub/index.php(37): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
#29 main


What am I doing wrong? What is the correct way to create a website programmatically?










share|improve this question
























  • try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

    – Marius
    Feb 26 at 18:00











  • Still getting the same error

    – coderGeek
    Feb 26 at 18:02













1












1








1








I'm using magento 2.3. I'm trying to create a website through a custom command. Here's my code



$website = $this->websiteFactory->create();
$website->setName("foo")->setCode("foobar");
$website->save();
var_dump("expression");


This gives an error - Validation Failed. In Save.php line 59.



However, it does create the website.



If I remove $website->save(), "expression" is displayed.



Stack trace:



Exception #0 (MagentoFrameworkValidationValidationException): Validation Failed
#0 vendor/magento/module-inventory/Model/StockRepository.php(67): MagentoInventoryModelStockCommandSave->execute(Object(MagentoInventoryModelStock))
#1 vendor/magento/framework/Interception/Interceptor.php(58): MagentoInventoryModelStockRepository->save(Object(MagentoInventoryModelStock))
#2 vendor/magento/framework/Interception/Interceptor.php(138): MagentoInventoryModelStockRepositoryInterceptor->___callParent('save', Array)
#3 vendor/magento/framework/Interception/Interceptor.php(153): MagentoInventoryModelStockRepositoryInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoInventoryModelStock))
#4 generated/code/Magento/Inventory/Model/StockRepository/Interceptor.php(26): MagentoInventoryModelStockRepositoryInterceptor->___callPlugins('save', Array, Array)
#5 vendor/magento/module-inventory-sales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php(100): MagentoInventoryModelStockRepositoryInterceptor->save(Object(MagentoInventoryModelStock))
#6 vendor/magento/framework/Interception/Interceptor.php(146): MagentoInventorySalesPluginStoreModelResourceModelWebsiteAssignWebsiteToDefaultStockPlugin->afterSave(Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelWebsiteInterceptor))
#7 vendor/magento/framework/Interception/Interceptor.php(153): MagentoStoreModelResourceModelWebsiteInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoStoreModelWebsiteInterceptor))
#8 generated/code/Magento/Store/Model/ResourceModel/Website/Interceptor.php(130): MagentoStoreModelResourceModelWebsiteInterceptor->___callPlugins('save', Array, NULL)
#9 app/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index.php(27): MagentoStoreModelResourceModelWebsiteInterceptor->save(Object(MagentoStoreModelWebsiteInterceptor))
#10 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(24): SampleModuleControllerAdminhtmlHelloWorldIndex->execute()
#11 vendor/magento/framework/App/Action/Action.php(108): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->execute()
#12 vendor/magento/module-backend/App/AbstractAction.php(231): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#13 vendor/magento/framework/Interception/Interceptor.php(58): MagentoBackendAppAbstractAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#14 vendor/magento/framework/Interception/Interceptor.php(138): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callParent('dispatch', Array)
#15 vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#16 vendor/magento/framework/Interception/Interceptor.php(135): MagentoBackendAppActionPluginAuthentication->aroundDispatch(Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#17 vendor/magento/framework/Interception/Interceptor.php(153): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#18 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(39): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callPlugins('dispatch', Array, NULL)
#19 vendor/magento/framework/App/FrontController.php(159): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#20 vendor/magento/framework/App/FrontController.php(99): MagentoFrameworkAppFrontController->processRequest(Object(MagentoFrameworkAppRequestHttp), Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor))
#21 vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
#22 vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
#23 vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#24 generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, Array)
#25 vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#26 generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
#27 vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttpInterceptor->launch()
#28 pub/index.php(37): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
#29 main


What am I doing wrong? What is the correct way to create a website programmatically?










share|improve this question
















I'm using magento 2.3. I'm trying to create a website through a custom command. Here's my code



$website = $this->websiteFactory->create();
$website->setName("foo")->setCode("foobar");
$website->save();
var_dump("expression");


This gives an error - Validation Failed. In Save.php line 59.



However, it does create the website.



If I remove $website->save(), "expression" is displayed.



Stack trace:



Exception #0 (MagentoFrameworkValidationValidationException): Validation Failed
#0 vendor/magento/module-inventory/Model/StockRepository.php(67): MagentoInventoryModelStockCommandSave->execute(Object(MagentoInventoryModelStock))
#1 vendor/magento/framework/Interception/Interceptor.php(58): MagentoInventoryModelStockRepository->save(Object(MagentoInventoryModelStock))
#2 vendor/magento/framework/Interception/Interceptor.php(138): MagentoInventoryModelStockRepositoryInterceptor->___callParent('save', Array)
#3 vendor/magento/framework/Interception/Interceptor.php(153): MagentoInventoryModelStockRepositoryInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoInventoryModelStock))
#4 generated/code/Magento/Inventory/Model/StockRepository/Interceptor.php(26): MagentoInventoryModelStockRepositoryInterceptor->___callPlugins('save', Array, Array)
#5 vendor/magento/module-inventory-sales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php(100): MagentoInventoryModelStockRepositoryInterceptor->save(Object(MagentoInventoryModelStock))
#6 vendor/magento/framework/Interception/Interceptor.php(146): MagentoInventorySalesPluginStoreModelResourceModelWebsiteAssignWebsiteToDefaultStockPlugin->afterSave(Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelResourceModelWebsiteInterceptor), Object(MagentoStoreModelWebsiteInterceptor))
#7 vendor/magento/framework/Interception/Interceptor.php(153): MagentoStoreModelResourceModelWebsiteInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoStoreModelWebsiteInterceptor))
#8 generated/code/Magento/Store/Model/ResourceModel/Website/Interceptor.php(130): MagentoStoreModelResourceModelWebsiteInterceptor->___callPlugins('save', Array, NULL)
#9 app/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index.php(27): MagentoStoreModelResourceModelWebsiteInterceptor->save(Object(MagentoStoreModelWebsiteInterceptor))
#10 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(24): SampleModuleControllerAdminhtmlHelloWorldIndex->execute()
#11 vendor/magento/framework/App/Action/Action.php(108): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->execute()
#12 vendor/magento/module-backend/App/AbstractAction.php(231): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#13 vendor/magento/framework/Interception/Interceptor.php(58): MagentoBackendAppAbstractAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#14 vendor/magento/framework/Interception/Interceptor.php(138): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callParent('dispatch', Array)
#15 vendor/magento/module-backend/App/Action/Plugin/Authentication.php(143): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#16 vendor/magento/framework/Interception/Interceptor.php(135): MagentoBackendAppActionPluginAuthentication->aroundDispatch(Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#17 vendor/magento/framework/Interception/Interceptor.php(153): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#18 generated/code/Sample/Module/Controller/Adminhtml/HelloWorld/Index/Interceptor.php(39): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->___callPlugins('dispatch', Array, NULL)
#19 vendor/magento/framework/App/FrontController.php(159): SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#20 vendor/magento/framework/App/FrontController.php(99): MagentoFrameworkAppFrontController->processRequest(Object(MagentoFrameworkAppRequestHttp), Object(SampleModuleControllerAdminhtmlHelloWorldIndexInterceptor))
#21 vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
#22 vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
#23 vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#24 generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, Array)
#25 vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#26 generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
#27 vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttpInterceptor->launch()
#28 pub/index.php(37): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
#29 main


What am I doing wrong? What is the correct way to create a website programmatically?







magento2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 20:07







coderGeek

















asked Feb 26 at 17:39









coderGeekcoderGeek

163




163












  • try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

    – Marius
    Feb 26 at 18:00











  • Still getting the same error

    – coderGeek
    Feb 26 at 18:02

















  • try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

    – Marius
    Feb 26 at 18:00











  • Still getting the same error

    – coderGeek
    Feb 26 at 18:02
















try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

– Marius
Feb 26 at 18:00





try setting a sort order and a default group id. probably 0 for group id ->setSortOrder(1)->setDefaultGroupId(0)

– Marius
Feb 26 at 18:00













Still getting the same error

– coderGeek
Feb 26 at 18:02





Still getting the same error

– coderGeek
Feb 26 at 18:02










2 Answers
2






active

oldest

votes


















0














Try :
$website = $this->websiteFactory->create();



$website->setCode('foobar');
$website->setName('foo Name');
$website->setDefaultGroupId(1);
$this->websiteResourceModel->save($website);


where $this->websiteResourceModel is MagentoStoreModelResourceModelWebsite






share|improve this answer























  • It isn't working. I have updated the question with the stack trace

    – coderGeek
    Feb 26 at 20:05


















0














I am having exactly the same issue - I have handled it by catching the exception and ignoring it if the website actually saved (because it does).



/**
* @namespace Namespace
* @module WebConfig
* @brief Module for Installation Scripts for Websites, Stores, Groups, etc.
*/

namespace NamespaceWebConfigSetup;

use MagentoFrameworkEventManagerInterface;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoStoreModelGroupFactory;
use MagentoStoreModelResourceModelGroup;
use MagentoStoreModelResourceModelStore;
use MagentoStoreModelResourceModelWebsite;
use MagentoStoreModelStoreFactory;
use MagentoStoreModelWebsiteFactory;
use MagentoConfigModelResourceModelConfig;
use MagentoFrameworkAppState;
use PsrLogLoggerInterface as Logger;

/**
* Class InstallData
*/
class InstallData implements InstallDataInterface

const WEBSITE_STORES
= array(
'Store One' => 'store_one',
'Store Two' => 'store_two',
);

/**
* @var WebsiteFactory
*/
private $websiteFactory;
/**
* @var Website
*/
private $websiteResourceModel;
/**
* @var StoreFactory
*/
private $storeFactory;
/**
* @var GroupFactory
*/
private $groupFactory;
/**
* @var Group
*/
private $groupResourceModel;
/**
* @var Store
*/
private $storeResourceModel;
/**
* @var ManagerInterface
*/
private $eventManager;

/**
* @var Config
*/
private $configResourceModel;

/**
* @var State
*/
private $appState;

/**
* @var Logger
*/
private $logger;

/**
* InstallData constructor.
*
* @param WebsiteFactory $websiteFactory
* @param Website $websiteResourceModel
* @param Store $storeResourceModel
* @param Group $groupResourceModel
* @param StoreFactory $storeFactory
* @param GroupFactory $groupFactory
* @param ManagerInterface $eventManager
* @param Config $configResourcModel
* @param Logger $logger
*/
public function __construct(
WebsiteFactory $websiteFactory,
Website $websiteResourceModel,
Store $storeResourceModel,
Group $groupResourceModel,
StoreFactory $storeFactory,
GroupFactory $groupFactory,
ManagerInterface $eventManager,
Config $configResourceModel,
State $appState,
Logger $logger
)
$this->websiteFactory = $websiteFactory;
$this->websiteResourceModel = $websiteResourceModel;
$this->storeFactory = $storeFactory;
$this->groupFactory = $groupFactory;
$this->groupResourceModel = $groupResourceModel;
$this->storeResourceModel = $storeResourceModel;
$this->eventManager = $eventManager;
$this->configResourceModel = $configResourceModel;
$this->appState = $appState;
$this->logger = $logger;


/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @return void
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
)
try
$this->appState->getAreaCode();
catch (MagentoFrameworkExceptionLocalizedException $ex)
$this->appState->setAreaCode('adminhtml');


$setup->startSetup();
$sortOrder = 0;

foreach (self::WEBSITE_STORES as $store_name => $store_code)

/** @var MagentoStoreModelWebsite $website */
$website = $this->websiteFactory->create();
$website->load($store_code);
if (!$website->getId())
$website->setCode($store_code);
$website->setName($store_name . ' Website');
$website->setDefaultGroupId(0);
$website->setSortOrder(0);
try
$this->websiteResourceModel->save($website);
catch (Exception $e)
$this->logger->error('fail to save website: ' . $website->getName() . ' ' . $e->getMessage());
if (!$website->getId())
//ignore if website actually did save
return;


else
//do not make store/group if website did not save
continue;

/** @var MagentoStoreModelStore $store */
$store = $this->storeFactory->create();
$store->load($store_code . '_store_view');
if (!$store->getId())
$store->setCode($store_code . '_store_view');
$store->setName($store_name . ' Store View');
$store->setWebsiteId($website->getId());
$store->setData('is_active', '1');
$store->setSortOrder($sortOrder);
try
$this->logger->info('trying to save store');
$this->storeResourceModel->save($store);
catch (Exception $e)
echo $e->getMessage();
$this->logger->error('fail to save store: ' . $store->getName() . ' ' . $e->getMessage());
if (!$store->getId())
return;


/* Config */
$this->configResourceModel
->saveConfig('design/header/logo_width', '173', 'stores', $store->getId())
->saveConfig('design/header/logo_height', '26', 'stores', $store->getId())
->saveConfig('design/theme/theme_id', '4', 'stores', $store->getId())
->saveConfig('design/header/logo_src', 'stores/' . $store->getId() . '/logo.png', 'stores',
$store->getId());
// 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]);

if ($website->getId())
/** @var MagentoStoreModelGroup $group */
$group = $this->groupFactory->create();
$group->setWebsiteId($website->getWebsiteId());
$group->setName($store_name . ' Store');
$group->setRootCategoryId(2);
$group->setDefaultStoreId($store->getId());
$group->setCode($store_code .'_store');
try
$this->groupResourceModel->save($group);
catch (Exception $e)
$this->logger->error('fail to save group: ' . $group->getName() . ' ' . $e->getMessage());
return;



$website->setDefaultGroupId($group->getId());
try
$this->websiteResourceModel->save($website);
catch (Exception $e)
$this->logger->error($e->getMessage());
if (!$website->getId())
return;


$store->setGroupId($group->getId());
try
$this->storeResourceModel->save($store);
catch (Exception $e)
echo $e->getMessage();
$this->logger->error('cannot save store group ' . $group->getId() . ' ' . $e->getMessage());

return;



$setup->endSetup();







share|improve this answer








New contributor




jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263532%2fcreating-website-programmatically%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try :
    $website = $this->websiteFactory->create();



    $website->setCode('foobar');
    $website->setName('foo Name');
    $website->setDefaultGroupId(1);
    $this->websiteResourceModel->save($website);


    where $this->websiteResourceModel is MagentoStoreModelResourceModelWebsite






    share|improve this answer























    • It isn't working. I have updated the question with the stack trace

      – coderGeek
      Feb 26 at 20:05















    0














    Try :
    $website = $this->websiteFactory->create();



    $website->setCode('foobar');
    $website->setName('foo Name');
    $website->setDefaultGroupId(1);
    $this->websiteResourceModel->save($website);


    where $this->websiteResourceModel is MagentoStoreModelResourceModelWebsite






    share|improve this answer























    • It isn't working. I have updated the question with the stack trace

      – coderGeek
      Feb 26 at 20:05













    0












    0








    0







    Try :
    $website = $this->websiteFactory->create();



    $website->setCode('foobar');
    $website->setName('foo Name');
    $website->setDefaultGroupId(1);
    $this->websiteResourceModel->save($website);


    where $this->websiteResourceModel is MagentoStoreModelResourceModelWebsite






    share|improve this answer













    Try :
    $website = $this->websiteFactory->create();



    $website->setCode('foobar');
    $website->setName('foo Name');
    $website->setDefaultGroupId(1);
    $this->websiteResourceModel->save($website);


    where $this->websiteResourceModel is MagentoStoreModelResourceModelWebsite







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 26 at 19:59









    VinzVinz

    483313




    483313












    • It isn't working. I have updated the question with the stack trace

      – coderGeek
      Feb 26 at 20:05

















    • It isn't working. I have updated the question with the stack trace

      – coderGeek
      Feb 26 at 20:05
















    It isn't working. I have updated the question with the stack trace

    – coderGeek
    Feb 26 at 20:05





    It isn't working. I have updated the question with the stack trace

    – coderGeek
    Feb 26 at 20:05













    0














    I am having exactly the same issue - I have handled it by catching the exception and ignoring it if the website actually saved (because it does).



    /**
    * @namespace Namespace
    * @module WebConfig
    * @brief Module for Installation Scripts for Websites, Stores, Groups, etc.
    */

    namespace NamespaceWebConfigSetup;

    use MagentoFrameworkEventManagerInterface;
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoStoreModelGroupFactory;
    use MagentoStoreModelResourceModelGroup;
    use MagentoStoreModelResourceModelStore;
    use MagentoStoreModelResourceModelWebsite;
    use MagentoStoreModelStoreFactory;
    use MagentoStoreModelWebsiteFactory;
    use MagentoConfigModelResourceModelConfig;
    use MagentoFrameworkAppState;
    use PsrLogLoggerInterface as Logger;

    /**
    * Class InstallData
    */
    class InstallData implements InstallDataInterface

    const WEBSITE_STORES
    = array(
    'Store One' => 'store_one',
    'Store Two' => 'store_two',
    );

    /**
    * @var WebsiteFactory
    */
    private $websiteFactory;
    /**
    * @var Website
    */
    private $websiteResourceModel;
    /**
    * @var StoreFactory
    */
    private $storeFactory;
    /**
    * @var GroupFactory
    */
    private $groupFactory;
    /**
    * @var Group
    */
    private $groupResourceModel;
    /**
    * @var Store
    */
    private $storeResourceModel;
    /**
    * @var ManagerInterface
    */
    private $eventManager;

    /**
    * @var Config
    */
    private $configResourceModel;

    /**
    * @var State
    */
    private $appState;

    /**
    * @var Logger
    */
    private $logger;

    /**
    * InstallData constructor.
    *
    * @param WebsiteFactory $websiteFactory
    * @param Website $websiteResourceModel
    * @param Store $storeResourceModel
    * @param Group $groupResourceModel
    * @param StoreFactory $storeFactory
    * @param GroupFactory $groupFactory
    * @param ManagerInterface $eventManager
    * @param Config $configResourcModel
    * @param Logger $logger
    */
    public function __construct(
    WebsiteFactory $websiteFactory,
    Website $websiteResourceModel,
    Store $storeResourceModel,
    Group $groupResourceModel,
    StoreFactory $storeFactory,
    GroupFactory $groupFactory,
    ManagerInterface $eventManager,
    Config $configResourceModel,
    State $appState,
    Logger $logger
    )
    $this->websiteFactory = $websiteFactory;
    $this->websiteResourceModel = $websiteResourceModel;
    $this->storeFactory = $storeFactory;
    $this->groupFactory = $groupFactory;
    $this->groupResourceModel = $groupResourceModel;
    $this->storeResourceModel = $storeResourceModel;
    $this->eventManager = $eventManager;
    $this->configResourceModel = $configResourceModel;
    $this->appState = $appState;
    $this->logger = $logger;


    /**
    * Installs data for a module
    *
    * @param ModuleDataSetupInterface $setup
    * @param ModuleContextInterface $context
    *
    * @return void
    */
    public function install(
    ModuleDataSetupInterface $setup,
    ModuleContextInterface $context
    )
    try
    $this->appState->getAreaCode();
    catch (MagentoFrameworkExceptionLocalizedException $ex)
    $this->appState->setAreaCode('adminhtml');


    $setup->startSetup();
    $sortOrder = 0;

    foreach (self::WEBSITE_STORES as $store_name => $store_code)

    /** @var MagentoStoreModelWebsite $website */
    $website = $this->websiteFactory->create();
    $website->load($store_code);
    if (!$website->getId())
    $website->setCode($store_code);
    $website->setName($store_name . ' Website');
    $website->setDefaultGroupId(0);
    $website->setSortOrder(0);
    try
    $this->websiteResourceModel->save($website);
    catch (Exception $e)
    $this->logger->error('fail to save website: ' . $website->getName() . ' ' . $e->getMessage());
    if (!$website->getId())
    //ignore if website actually did save
    return;


    else
    //do not make store/group if website did not save
    continue;

    /** @var MagentoStoreModelStore $store */
    $store = $this->storeFactory->create();
    $store->load($store_code . '_store_view');
    if (!$store->getId())
    $store->setCode($store_code . '_store_view');
    $store->setName($store_name . ' Store View');
    $store->setWebsiteId($website->getId());
    $store->setData('is_active', '1');
    $store->setSortOrder($sortOrder);
    try
    $this->logger->info('trying to save store');
    $this->storeResourceModel->save($store);
    catch (Exception $e)
    echo $e->getMessage();
    $this->logger->error('fail to save store: ' . $store->getName() . ' ' . $e->getMessage());
    if (!$store->getId())
    return;


    /* Config */
    $this->configResourceModel
    ->saveConfig('design/header/logo_width', '173', 'stores', $store->getId())
    ->saveConfig('design/header/logo_height', '26', 'stores', $store->getId())
    ->saveConfig('design/theme/theme_id', '4', 'stores', $store->getId())
    ->saveConfig('design/header/logo_src', 'stores/' . $store->getId() . '/logo.png', 'stores',
    $store->getId());
    // 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]);

    if ($website->getId())
    /** @var MagentoStoreModelGroup $group */
    $group = $this->groupFactory->create();
    $group->setWebsiteId($website->getWebsiteId());
    $group->setName($store_name . ' Store');
    $group->setRootCategoryId(2);
    $group->setDefaultStoreId($store->getId());
    $group->setCode($store_code .'_store');
    try
    $this->groupResourceModel->save($group);
    catch (Exception $e)
    $this->logger->error('fail to save group: ' . $group->getName() . ' ' . $e->getMessage());
    return;



    $website->setDefaultGroupId($group->getId());
    try
    $this->websiteResourceModel->save($website);
    catch (Exception $e)
    $this->logger->error($e->getMessage());
    if (!$website->getId())
    return;


    $store->setGroupId($group->getId());
    try
    $this->storeResourceModel->save($store);
    catch (Exception $e)
    echo $e->getMessage();
    $this->logger->error('cannot save store group ' . $group->getId() . ' ' . $e->getMessage());

    return;



    $setup->endSetup();







    share|improve this answer








    New contributor




    jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.
























      0














      I am having exactly the same issue - I have handled it by catching the exception and ignoring it if the website actually saved (because it does).



      /**
      * @namespace Namespace
      * @module WebConfig
      * @brief Module for Installation Scripts for Websites, Stores, Groups, etc.
      */

      namespace NamespaceWebConfigSetup;

      use MagentoFrameworkEventManagerInterface;
      use MagentoFrameworkSetupInstallDataInterface;
      use MagentoFrameworkSetupModuleContextInterface;
      use MagentoFrameworkSetupModuleDataSetupInterface;
      use MagentoStoreModelGroupFactory;
      use MagentoStoreModelResourceModelGroup;
      use MagentoStoreModelResourceModelStore;
      use MagentoStoreModelResourceModelWebsite;
      use MagentoStoreModelStoreFactory;
      use MagentoStoreModelWebsiteFactory;
      use MagentoConfigModelResourceModelConfig;
      use MagentoFrameworkAppState;
      use PsrLogLoggerInterface as Logger;

      /**
      * Class InstallData
      */
      class InstallData implements InstallDataInterface

      const WEBSITE_STORES
      = array(
      'Store One' => 'store_one',
      'Store Two' => 'store_two',
      );

      /**
      * @var WebsiteFactory
      */
      private $websiteFactory;
      /**
      * @var Website
      */
      private $websiteResourceModel;
      /**
      * @var StoreFactory
      */
      private $storeFactory;
      /**
      * @var GroupFactory
      */
      private $groupFactory;
      /**
      * @var Group
      */
      private $groupResourceModel;
      /**
      * @var Store
      */
      private $storeResourceModel;
      /**
      * @var ManagerInterface
      */
      private $eventManager;

      /**
      * @var Config
      */
      private $configResourceModel;

      /**
      * @var State
      */
      private $appState;

      /**
      * @var Logger
      */
      private $logger;

      /**
      * InstallData constructor.
      *
      * @param WebsiteFactory $websiteFactory
      * @param Website $websiteResourceModel
      * @param Store $storeResourceModel
      * @param Group $groupResourceModel
      * @param StoreFactory $storeFactory
      * @param GroupFactory $groupFactory
      * @param ManagerInterface $eventManager
      * @param Config $configResourcModel
      * @param Logger $logger
      */
      public function __construct(
      WebsiteFactory $websiteFactory,
      Website $websiteResourceModel,
      Store $storeResourceModel,
      Group $groupResourceModel,
      StoreFactory $storeFactory,
      GroupFactory $groupFactory,
      ManagerInterface $eventManager,
      Config $configResourceModel,
      State $appState,
      Logger $logger
      )
      $this->websiteFactory = $websiteFactory;
      $this->websiteResourceModel = $websiteResourceModel;
      $this->storeFactory = $storeFactory;
      $this->groupFactory = $groupFactory;
      $this->groupResourceModel = $groupResourceModel;
      $this->storeResourceModel = $storeResourceModel;
      $this->eventManager = $eventManager;
      $this->configResourceModel = $configResourceModel;
      $this->appState = $appState;
      $this->logger = $logger;


      /**
      * Installs data for a module
      *
      * @param ModuleDataSetupInterface $setup
      * @param ModuleContextInterface $context
      *
      * @return void
      */
      public function install(
      ModuleDataSetupInterface $setup,
      ModuleContextInterface $context
      )
      try
      $this->appState->getAreaCode();
      catch (MagentoFrameworkExceptionLocalizedException $ex)
      $this->appState->setAreaCode('adminhtml');


      $setup->startSetup();
      $sortOrder = 0;

      foreach (self::WEBSITE_STORES as $store_name => $store_code)

      /** @var MagentoStoreModelWebsite $website */
      $website = $this->websiteFactory->create();
      $website->load($store_code);
      if (!$website->getId())
      $website->setCode($store_code);
      $website->setName($store_name . ' Website');
      $website->setDefaultGroupId(0);
      $website->setSortOrder(0);
      try
      $this->websiteResourceModel->save($website);
      catch (Exception $e)
      $this->logger->error('fail to save website: ' . $website->getName() . ' ' . $e->getMessage());
      if (!$website->getId())
      //ignore if website actually did save
      return;


      else
      //do not make store/group if website did not save
      continue;

      /** @var MagentoStoreModelStore $store */
      $store = $this->storeFactory->create();
      $store->load($store_code . '_store_view');
      if (!$store->getId())
      $store->setCode($store_code . '_store_view');
      $store->setName($store_name . ' Store View');
      $store->setWebsiteId($website->getId());
      $store->setData('is_active', '1');
      $store->setSortOrder($sortOrder);
      try
      $this->logger->info('trying to save store');
      $this->storeResourceModel->save($store);
      catch (Exception $e)
      echo $e->getMessage();
      $this->logger->error('fail to save store: ' . $store->getName() . ' ' . $e->getMessage());
      if (!$store->getId())
      return;


      /* Config */
      $this->configResourceModel
      ->saveConfig('design/header/logo_width', '173', 'stores', $store->getId())
      ->saveConfig('design/header/logo_height', '26', 'stores', $store->getId())
      ->saveConfig('design/theme/theme_id', '4', 'stores', $store->getId())
      ->saveConfig('design/header/logo_src', 'stores/' . $store->getId() . '/logo.png', 'stores',
      $store->getId());
      // 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]);

      if ($website->getId())
      /** @var MagentoStoreModelGroup $group */
      $group = $this->groupFactory->create();
      $group->setWebsiteId($website->getWebsiteId());
      $group->setName($store_name . ' Store');
      $group->setRootCategoryId(2);
      $group->setDefaultStoreId($store->getId());
      $group->setCode($store_code .'_store');
      try
      $this->groupResourceModel->save($group);
      catch (Exception $e)
      $this->logger->error('fail to save group: ' . $group->getName() . ' ' . $e->getMessage());
      return;



      $website->setDefaultGroupId($group->getId());
      try
      $this->websiteResourceModel->save($website);
      catch (Exception $e)
      $this->logger->error($e->getMessage());
      if (!$website->getId())
      return;


      $store->setGroupId($group->getId());
      try
      $this->storeResourceModel->save($store);
      catch (Exception $e)
      echo $e->getMessage();
      $this->logger->error('cannot save store group ' . $group->getId() . ' ' . $e->getMessage());

      return;



      $setup->endSetup();







      share|improve this answer








      New contributor




      jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        0












        0








        0







        I am having exactly the same issue - I have handled it by catching the exception and ignoring it if the website actually saved (because it does).



        /**
        * @namespace Namespace
        * @module WebConfig
        * @brief Module for Installation Scripts for Websites, Stores, Groups, etc.
        */

        namespace NamespaceWebConfigSetup;

        use MagentoFrameworkEventManagerInterface;
        use MagentoFrameworkSetupInstallDataInterface;
        use MagentoFrameworkSetupModuleContextInterface;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoStoreModelGroupFactory;
        use MagentoStoreModelResourceModelGroup;
        use MagentoStoreModelResourceModelStore;
        use MagentoStoreModelResourceModelWebsite;
        use MagentoStoreModelStoreFactory;
        use MagentoStoreModelWebsiteFactory;
        use MagentoConfigModelResourceModelConfig;
        use MagentoFrameworkAppState;
        use PsrLogLoggerInterface as Logger;

        /**
        * Class InstallData
        */
        class InstallData implements InstallDataInterface

        const WEBSITE_STORES
        = array(
        'Store One' => 'store_one',
        'Store Two' => 'store_two',
        );

        /**
        * @var WebsiteFactory
        */
        private $websiteFactory;
        /**
        * @var Website
        */
        private $websiteResourceModel;
        /**
        * @var StoreFactory
        */
        private $storeFactory;
        /**
        * @var GroupFactory
        */
        private $groupFactory;
        /**
        * @var Group
        */
        private $groupResourceModel;
        /**
        * @var Store
        */
        private $storeResourceModel;
        /**
        * @var ManagerInterface
        */
        private $eventManager;

        /**
        * @var Config
        */
        private $configResourceModel;

        /**
        * @var State
        */
        private $appState;

        /**
        * @var Logger
        */
        private $logger;

        /**
        * InstallData constructor.
        *
        * @param WebsiteFactory $websiteFactory
        * @param Website $websiteResourceModel
        * @param Store $storeResourceModel
        * @param Group $groupResourceModel
        * @param StoreFactory $storeFactory
        * @param GroupFactory $groupFactory
        * @param ManagerInterface $eventManager
        * @param Config $configResourcModel
        * @param Logger $logger
        */
        public function __construct(
        WebsiteFactory $websiteFactory,
        Website $websiteResourceModel,
        Store $storeResourceModel,
        Group $groupResourceModel,
        StoreFactory $storeFactory,
        GroupFactory $groupFactory,
        ManagerInterface $eventManager,
        Config $configResourceModel,
        State $appState,
        Logger $logger
        )
        $this->websiteFactory = $websiteFactory;
        $this->websiteResourceModel = $websiteResourceModel;
        $this->storeFactory = $storeFactory;
        $this->groupFactory = $groupFactory;
        $this->groupResourceModel = $groupResourceModel;
        $this->storeResourceModel = $storeResourceModel;
        $this->eventManager = $eventManager;
        $this->configResourceModel = $configResourceModel;
        $this->appState = $appState;
        $this->logger = $logger;


        /**
        * Installs data for a module
        *
        * @param ModuleDataSetupInterface $setup
        * @param ModuleContextInterface $context
        *
        * @return void
        */
        public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
        )
        try
        $this->appState->getAreaCode();
        catch (MagentoFrameworkExceptionLocalizedException $ex)
        $this->appState->setAreaCode('adminhtml');


        $setup->startSetup();
        $sortOrder = 0;

        foreach (self::WEBSITE_STORES as $store_name => $store_code)

        /** @var MagentoStoreModelWebsite $website */
        $website = $this->websiteFactory->create();
        $website->load($store_code);
        if (!$website->getId())
        $website->setCode($store_code);
        $website->setName($store_name . ' Website');
        $website->setDefaultGroupId(0);
        $website->setSortOrder(0);
        try
        $this->websiteResourceModel->save($website);
        catch (Exception $e)
        $this->logger->error('fail to save website: ' . $website->getName() . ' ' . $e->getMessage());
        if (!$website->getId())
        //ignore if website actually did save
        return;


        else
        //do not make store/group if website did not save
        continue;

        /** @var MagentoStoreModelStore $store */
        $store = $this->storeFactory->create();
        $store->load($store_code . '_store_view');
        if (!$store->getId())
        $store->setCode($store_code . '_store_view');
        $store->setName($store_name . ' Store View');
        $store->setWebsiteId($website->getId());
        $store->setData('is_active', '1');
        $store->setSortOrder($sortOrder);
        try
        $this->logger->info('trying to save store');
        $this->storeResourceModel->save($store);
        catch (Exception $e)
        echo $e->getMessage();
        $this->logger->error('fail to save store: ' . $store->getName() . ' ' . $e->getMessage());
        if (!$store->getId())
        return;


        /* Config */
        $this->configResourceModel
        ->saveConfig('design/header/logo_width', '173', 'stores', $store->getId())
        ->saveConfig('design/header/logo_height', '26', 'stores', $store->getId())
        ->saveConfig('design/theme/theme_id', '4', 'stores', $store->getId())
        ->saveConfig('design/header/logo_src', 'stores/' . $store->getId() . '/logo.png', 'stores',
        $store->getId());
        // 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]);

        if ($website->getId())
        /** @var MagentoStoreModelGroup $group */
        $group = $this->groupFactory->create();
        $group->setWebsiteId($website->getWebsiteId());
        $group->setName($store_name . ' Store');
        $group->setRootCategoryId(2);
        $group->setDefaultStoreId($store->getId());
        $group->setCode($store_code .'_store');
        try
        $this->groupResourceModel->save($group);
        catch (Exception $e)
        $this->logger->error('fail to save group: ' . $group->getName() . ' ' . $e->getMessage());
        return;



        $website->setDefaultGroupId($group->getId());
        try
        $this->websiteResourceModel->save($website);
        catch (Exception $e)
        $this->logger->error($e->getMessage());
        if (!$website->getId())
        return;


        $store->setGroupId($group->getId());
        try
        $this->storeResourceModel->save($store);
        catch (Exception $e)
        echo $e->getMessage();
        $this->logger->error('cannot save store group ' . $group->getId() . ' ' . $e->getMessage());

        return;



        $setup->endSetup();







        share|improve this answer








        New contributor




        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        I am having exactly the same issue - I have handled it by catching the exception and ignoring it if the website actually saved (because it does).



        /**
        * @namespace Namespace
        * @module WebConfig
        * @brief Module for Installation Scripts for Websites, Stores, Groups, etc.
        */

        namespace NamespaceWebConfigSetup;

        use MagentoFrameworkEventManagerInterface;
        use MagentoFrameworkSetupInstallDataInterface;
        use MagentoFrameworkSetupModuleContextInterface;
        use MagentoFrameworkSetupModuleDataSetupInterface;
        use MagentoStoreModelGroupFactory;
        use MagentoStoreModelResourceModelGroup;
        use MagentoStoreModelResourceModelStore;
        use MagentoStoreModelResourceModelWebsite;
        use MagentoStoreModelStoreFactory;
        use MagentoStoreModelWebsiteFactory;
        use MagentoConfigModelResourceModelConfig;
        use MagentoFrameworkAppState;
        use PsrLogLoggerInterface as Logger;

        /**
        * Class InstallData
        */
        class InstallData implements InstallDataInterface

        const WEBSITE_STORES
        = array(
        'Store One' => 'store_one',
        'Store Two' => 'store_two',
        );

        /**
        * @var WebsiteFactory
        */
        private $websiteFactory;
        /**
        * @var Website
        */
        private $websiteResourceModel;
        /**
        * @var StoreFactory
        */
        private $storeFactory;
        /**
        * @var GroupFactory
        */
        private $groupFactory;
        /**
        * @var Group
        */
        private $groupResourceModel;
        /**
        * @var Store
        */
        private $storeResourceModel;
        /**
        * @var ManagerInterface
        */
        private $eventManager;

        /**
        * @var Config
        */
        private $configResourceModel;

        /**
        * @var State
        */
        private $appState;

        /**
        * @var Logger
        */
        private $logger;

        /**
        * InstallData constructor.
        *
        * @param WebsiteFactory $websiteFactory
        * @param Website $websiteResourceModel
        * @param Store $storeResourceModel
        * @param Group $groupResourceModel
        * @param StoreFactory $storeFactory
        * @param GroupFactory $groupFactory
        * @param ManagerInterface $eventManager
        * @param Config $configResourcModel
        * @param Logger $logger
        */
        public function __construct(
        WebsiteFactory $websiteFactory,
        Website $websiteResourceModel,
        Store $storeResourceModel,
        Group $groupResourceModel,
        StoreFactory $storeFactory,
        GroupFactory $groupFactory,
        ManagerInterface $eventManager,
        Config $configResourceModel,
        State $appState,
        Logger $logger
        )
        $this->websiteFactory = $websiteFactory;
        $this->websiteResourceModel = $websiteResourceModel;
        $this->storeFactory = $storeFactory;
        $this->groupFactory = $groupFactory;
        $this->groupResourceModel = $groupResourceModel;
        $this->storeResourceModel = $storeResourceModel;
        $this->eventManager = $eventManager;
        $this->configResourceModel = $configResourceModel;
        $this->appState = $appState;
        $this->logger = $logger;


        /**
        * Installs data for a module
        *
        * @param ModuleDataSetupInterface $setup
        * @param ModuleContextInterface $context
        *
        * @return void
        */
        public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
        )
        try
        $this->appState->getAreaCode();
        catch (MagentoFrameworkExceptionLocalizedException $ex)
        $this->appState->setAreaCode('adminhtml');


        $setup->startSetup();
        $sortOrder = 0;

        foreach (self::WEBSITE_STORES as $store_name => $store_code)

        /** @var MagentoStoreModelWebsite $website */
        $website = $this->websiteFactory->create();
        $website->load($store_code);
        if (!$website->getId())
        $website->setCode($store_code);
        $website->setName($store_name . ' Website');
        $website->setDefaultGroupId(0);
        $website->setSortOrder(0);
        try
        $this->websiteResourceModel->save($website);
        catch (Exception $e)
        $this->logger->error('fail to save website: ' . $website->getName() . ' ' . $e->getMessage());
        if (!$website->getId())
        //ignore if website actually did save
        return;


        else
        //do not make store/group if website did not save
        continue;

        /** @var MagentoStoreModelStore $store */
        $store = $this->storeFactory->create();
        $store->load($store_code . '_store_view');
        if (!$store->getId())
        $store->setCode($store_code . '_store_view');
        $store->setName($store_name . ' Store View');
        $store->setWebsiteId($website->getId());
        $store->setData('is_active', '1');
        $store->setSortOrder($sortOrder);
        try
        $this->logger->info('trying to save store');
        $this->storeResourceModel->save($store);
        catch (Exception $e)
        echo $e->getMessage();
        $this->logger->error('fail to save store: ' . $store->getName() . ' ' . $e->getMessage());
        if (!$store->getId())
        return;


        /* Config */
        $this->configResourceModel
        ->saveConfig('design/header/logo_width', '173', 'stores', $store->getId())
        ->saveConfig('design/header/logo_height', '26', 'stores', $store->getId())
        ->saveConfig('design/theme/theme_id', '4', 'stores', $store->getId())
        ->saveConfig('design/header/logo_src', 'stores/' . $store->getId() . '/logo.png', 'stores',
        $store->getId());
        // 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]);

        if ($website->getId())
        /** @var MagentoStoreModelGroup $group */
        $group = $this->groupFactory->create();
        $group->setWebsiteId($website->getWebsiteId());
        $group->setName($store_name . ' Store');
        $group->setRootCategoryId(2);
        $group->setDefaultStoreId($store->getId());
        $group->setCode($store_code .'_store');
        try
        $this->groupResourceModel->save($group);
        catch (Exception $e)
        $this->logger->error('fail to save group: ' . $group->getName() . ' ' . $e->getMessage());
        return;



        $website->setDefaultGroupId($group->getId());
        try
        $this->websiteResourceModel->save($website);
        catch (Exception $e)
        $this->logger->error($e->getMessage());
        if (!$website->getId())
        return;


        $store->setGroupId($group->getId());
        try
        $this->storeResourceModel->save($store);
        catch (Exception $e)
        echo $e->getMessage();
        $this->logger->error('cannot save store group ' . $group->getId() . ' ' . $e->getMessage());

        return;



        $setup->endSetup();








        share|improve this answer








        New contributor




        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 13 mins ago









        jnodwelljnodwell

        11




        11




        New contributor




        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        jnodwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263532%2fcreating-website-programmatically%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

            How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2

            Nissan Patrol Зміст Перше покоління — 4W60 (1951-1960) | Друге покоління — 60 series (1960-1980) | Третє покоління (1980–2002) | Четверте покоління — Y60 (1987–1998) | П'яте покоління — Y61 (1997–2013) | Шосте покоління — Y62 (2010- ) | Посилання | Зноски | Навігаційне менюОфіційний український сайтТест-драйв Nissan Patrol 2010 7-го поколінняNissan PatrolКак мы тестировали Nissan Patrol 2016рвиправивши або дописавши її