Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How To Add Store View Selection In Magento Admin Grid?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.38k
    Comment on it

    Hello Readers,  
    In this tutorial, I will explain how to add Store View selection to Module’s Adminhtml
    This article assumes you already know how to create Magento Admin grid.

    Go to Namespace/Module/Block/Adminhtml/Module/Edit/Tab/Form.php
    Add below code in _prepareForm() function.

    1. if (!Mage::app()->isSingleStoreMode()) {
    2. $fieldset->addField('stores', 'multiselect', array(
    3. 'name' => 'stores[]',
    4. 'label' => Mage::helper('slideshow')->__('Select Store'),
    5. 'title' => Mage::helper('slideshow')->__('Select Store'),
    6. 'required' => true,
    7. 'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(true, true),
    8. ));
    9. }
    10. else {
    11. $fieldset->addField('stores', 'hidden', array(
    12. 'name' => 'stores[]',
    13. 'value' => Mage::app()->getStore(true)->getId()
    14. ));
    15. }

    Above Code show store view multiselect option to your form. This will work, if you have multiple stores in you setup.

    Next, Go to Namespace/Module/Adminhtml/IndexController.php
    Add below code in saveAction() function.

    1. if(isset($postData['stores']))
    2. {
    3. if(in_array('0',$postData['stores'])){
    4. $postData['stores'] = '0';
    5. }
    6. else{
    7. $postData['stores'] = implode(",", $postData['stores']);
    8. }
    9. //unset($postData['stores']);
    10. }
    11. if($postData['stores'] == "")
    12. {
    13. $postData['stores'] = '0';
    14. }

    The above code will set stores to 0 if “All Store Views” was selected or sets stores as comma-seperated values using implode function.

    Next, In your Custom modules Grid file (Path: Namespace/Module/Block/Adminhtml/Module/Grid.php).
    Add the following code.

    1. protected function _prepareCollection()
    2. {
    3. $collection = Mage::getModel('slideshow/slideshow')->getCollection();
    4.  
    5. // echo '<pre>'; print_r($collection->getData()); die;
    6.  
    7. foreach($collection as $link){
    8. if($link->getStores() && $link->getStores() != 0 ){
    9. $link->setStores(explode(',',$link->getStores()));
    10. }
    11. else{
    12. $link->setStores(array('0'));
    13. }
    14. }
    15. $this->setCollection($collection);
    16.  
    17. return parent::_prepareCollection();
    18. }

    setStores(array('0')). Here '0' represents All Store Views.

    In the same class file (i.e Grid.php), add the following code in prepareColumns() function

    1. if (!Mage::app()->isSingleStoreMode()) {
    2. $this->addColumn('stores', array(
    3. 'header' => Mage::helper('')->__('Store View'),
    4. 'index' => 'stores',
    5. 'type' => 'store',
    6. 'store_all' => true,
    7. 'store_view' => true,
    8. 'sortable' => true,
    9. 'filter_condition_callback' => array($this,
    10. '_filterStoreCondition'),
    11. ));
    12. }

    & add the following function in Grid.php:

    1. protected function _filterStoreCondition($collection, $column){
    2. if (!$value = $column->getFilter()->getValue()) {
    3. return;
    4. }
    5. $this->getCollection()->addStoreFilter($value);
    6. }

    _filterStoreCondition($collection, $column): This function checks store filter has been selected or not and if so , then it will call add the filter function to the collection.

    Lastly, Go to Collection file (Path: Namespace_Module_Model_Mysql4_Module_Collection) & add this function in collection.php

    1. public function addStoreFilter($store, $withAdmin = true){
    2.  
    3. if ($store instanceof Mage_Core_Model_Store) {
    4. $store = array($store->getId());
    5. }
    6.  
    7. if (!is_array($store)) {
    8. $store = array($store);
    9. }
    10.  
    11. $this->addFilter('stores', array('in' => $store));
    12.  
    13. return $this;
    14. }

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: