2

I ve been looking around for a good solution to display the store view in a column of my admin grid.

I have the following in my grid

class NsTest_Test_Block_Adminhtml_Testimonial_Grid extends Mage_Adminhtml_Block_Widget_Grid {
...

  protected function _prepareCollection()
  {
      $collection = Mage::getModel('test/testimonial')->getCollection();

    $this->setCollection($collection);
    return parent::_prepareCollection();
}


...

I read it is possible to add the store view with this code from some block on internet

$collection = Mage::getModel('tc_storeview/stores')->getResourceCollection();

foreach ($collection as $view) {
    if ( $view->getStoreId() && $view->getStoreId() != 0 ) {
        $view->setStoreId(explode(',',$view->getStoreId()));
    } else {
        $view->setStoreId(array('0'));
    }
}

But how to use this? I think the code would show the store view, but how about my data. Is there a way to join both, store and my model ?


Seems like below code is working, still dont really understand how that can work.

class NsTest_Test_Block_Adminhtml_Testimonial_Grid extends Mage_Adminhtml_Block_Widget_Grid {
...

  protected function _prepareCollection()
  {
      $collection = Mage::getModel('test/testimonial')->getCollection();

foreach ($collection as $view) {
    if ( $view->getStoreId() && $view->getStoreId() != 0 ) {
        $view->setStoreId(explode(',',$view->getStoreId()));
    } else {
        $view->setStoreId(array('0'));
    }
}

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

2 Answers 2

7

for store grind column use below code. store is database coulmn name

 $this->addColumn('store', array( 
                    'header' => 'Website', 
                    'index' => 'store', 
                    'type' => 'store', 
                    'width' => '100px', 
                    'store_view'=> true, 
                    'display_deleted' => false, 
                    'renderer' => 'Namespace_Modulename_Block_Adminhtml_Store',
                    )); 

for rendering the stores

<?php 

class Namespcae_Modulename_Block_Adminhtml_Store
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row) 
    {    $store=explode(',',$row->getStore());

            $data="";
         if($row->getStore()!="" and $row->getStore()==0)
         { 
            $allstore=Mage::app()->getStores();
            foreach($allstore as $astore)
            {
                $data.=$astore->getName().'<br />';
            }

             } else {
                $data="";
                $a=0;
                foreach ($store as $sto)
                {
                     $data= $data.Mage::getModel('core/store')->load($sto[$a])->getName().'<br>';
                 $a+1;}
             }

        return $data;
    }
} 
1
  • I would just make a modification to be able to use the correct data if your index is not the store. Replace $store=explode(',',$row->getStore()); with $index = $this->getColumn()->getIndex(); $store = explode(',', $row->getData($index)); Jul 12, 2018 at 18:26
0

Follow this below tutorial and it will help you to achieve your requirement.

http://www.demacmedia.com/magento-commerce/mini-tutorial-adding-store-view-selection-to-your-modules-adminhtml/

3
  • not helping me because i dont understand how this $collection = Mage::getModel('_/')->getCollection(); works. My understanding is that a collection is a set of Models. Since my model has specific columns, I think I need to Add the store model into the collection to be able to have the link with store information. Nov 25, 2015 at 5:00
  • Or maybe Magento is automagic, if in my model there is the store_id, it can automatically find out the right table and make the link ? Seems like Nov 25, 2015 at 5:03
  • $collection = Mage::getModel('module/model')->getCollection(); Nov 25, 2015 at 5:21

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.