0

Here is my script:

<?php

namespace WatchUWant\ConfigSync\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Store\Model;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Catalog\Helper\DefaultCategory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Backend\Controller\Adminhtml\System\Store;
/*
class StoreObject extends Store {

}
*/

//include('/vendor/magento/module-store/Model/Store.php');
class InstallSchema implements InstallSchemaInterface
{


    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {

     $installer = $setup;

        $installer->startSetup();

//  $connection = $installer->getConnection();

    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 // $storeModel=$om->get('Magento\Store\Model\Store');

        $i=0;
        $code_found=false;
        $key_not_found=false;
/*        while($code_found == false && $key_not_found==false)  {

            $storeModel->load($i);

            $code = $storeModel->getCode();

            if ($code == 'hong-kong')   {
                $code_found=true;
            }

            if(empty($code))    {
                $key_not_found=true;
            }   else    {
                $i++;
        }
        }
   */     


   // GROUP (STORE) ARRAY Array ( [website_id] => 1 [name] => Hong Kong Store [root_category_id] => 2 [group_id] => )

    $group_array = Array();
    $group_array['group']= Array('website_id'=>'1','name'=>'Honk Kong','root_category_id'=>'2','group_id'=>'');


    $group_array['group']['name'] = $this->filterManager->removeTags($group_array['group']['name']);
    /** @var \Magento\Store\Model\Group $groupModel */
    $groupModel = $this->_objectManager->create('Magento\Store\Model\Group');


    $all_stores=$group_model->getStores();

    $already_added=false;
/*  foreach ($all_stores as $a_store)   {
        if($a_store['code']=='hong_kong')   {
            $already_added=true;
        }
    }
*/  
//  if($already_added==false)   {

        if ($group_array['group']['group_id']) {
            $groupModel->load($group_array['group']['group_id']);
        }
        $groupModel->setData($group_array['group']);
        if ($group_array['group']['group_id'] == '') {
            $groupModel->setId(null);
        }
        if (!$this->isSelectedDefaultStoreActive($group_array, $groupModel)) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('An inactive store view cannot be saved as default store view')
            );
        }
        $groupModel->save();
        $this->_eventManager->dispatch('store_group_save', ['group' => $groupModel]);
//      $this->messageManager->addSuccess(__('You saved the store.'));

        // END GROUP

        // STORE VIEW ARRAY group_id (store), name, code, is_active, sort_order

        $store_ids = $groupModel->getStoreIds();

        $group_id= max($store_ids);

        $store_array = Array();
        $store_array['store']= Array('group_id'=>$group_id,'name'=>'Honk Kong','code'=>'hong_kong_view','is_active'=>'1','sort_order'=>'0','store_id'=>'');


        $eventName = 'store_edit';
        /** @var \Magento\Store\Model\Store $storeModel */
        $storeModel = $this->_objectManager->create('Magento\Store\Model\Store');
        $store_array['store']['name'] = $this->filterManager->removeTags($store_array['store']['name']);
        if ($store_array['store']['store_id']) {
            $storeModel->load($store_array['store']['store_id']);
        }
        $storeModel->setData($store_array['store']);
        if ($store_array['store']['store_id'] == '') {
            $storeModel->setId(null);
            $eventName = 'store_add';
        }
        $groupModel = $this->_objectManager->create(
            'Magento\Store\Model\Group'
        )->load(
            $storeModel->getGroupId()
        );
        $storeModel->setWebsiteId($groupModel->getWebsiteId());
        if (!$storeModel->isActive() && $storeModel->isDefault()) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('The default store cannot be disabled')
            );
        }
        $storeModel->save();
        $this->_objectManager->get('Magento\Store\Model\StoreManager')->reinitStores();
        $this->_eventManager->dispatch($eventName, ['store' => $storeModel]);
//      $this->messageManager->addSuccess(__('You saved the store view.'.));

            //END STORE VIEW
//      }

   /*
    $postData['website']['name'] = $this->filterManager->removeTags($postData['website']['name']);
    $websiteModel = $this->_objectManager->create('Magento\Store\Model\Website');
    if ($postData['website']['website_id']) {
        $websiteModel->load($postData['website']['website_id']);
    }
    $websiteModel->setData($postData['website']);
    if ($postData['website']['website_id'] == '') {
        $websiteModel->setId(null);
    }

    $websiteModel->save();

*/
        $installer->endSetup();
    }
}

But is doesn't seem to be running the install function in that class. Any help would be much appreciated!

0

I solved the problem by changing the file name from "installData.php" to "installSchema.php"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.