1

I want to retrieve all stores and store views for a given website id. The data is to be shown as a hierarchy in multi select dropdown in the admin form. I tried using \Magento\Store\Model\StoreManagerInterface to retrieve stores as follows:

$storeData = $this->StoreManagerInterface->getStores();

The above only gives the details about all the stores. But I am not able to retrieve stores through website Id.

3
  • Where you want to get this details? Oct 23, 2018 at 9:46
  • In custom admin form multi-select dropdown.
    – GenZ Dev
    Oct 23, 2018 at 9:49
  • Please check my answer Oct 23, 2018 at 9:54

1 Answer 1

0

You should inject class Magento\Store\Model\System\Store in the constructor.

protected $_systemStore;

public function __construct(
   ....
   \Magento\Store\Model\System\Store $systemStore
   ....
) {
  ....
  $this->_systemStore = $systemStore;
  ....
}

protected function _prepareForm()
{
    ....
    $fieldset->addField(
       'store_ids',
       'multiselect',
       [
         'name'     => 'store_ids[]',
         'label'    => __('Store Views'),
         'title'    => __('Store Views'),
         'required' => true,
         'values'   => $this->_systemStore->getStoreValuesForForm(false, true),
       ]
    );
}

I hope it will helpful.

1
  • Happy to help !! Happy coding :) Oct 23, 2018 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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