2016-08-24 53 views

回答

2

首先,要將新列添加到現有產品管理表,您必須擴展magento塊:Mage_Adminhtml_Block_Catalog_Product_Grid。 爲此,您可以創建自定義模塊fe。名爲XXX在你的config.xml文件將這些行:

<global> 
    <blocks> 
     <adminhtml> 
      <rewrite> 
       <catalog_product_grid>XXX_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid> 
      </rewrite> 
     </adminhtml> 
    </blocks> 
</global> 

現在,在你的文件XXX_Adminhtml_Block_Catalog_Product_Grid你需要覆蓋兩個方法:_prepareCollection()

class XXX_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid { 
    // ... 
    protected function _prepareCollection() 
    { 
     //... 
     $collection = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect('sku') 
      ->addAttributeToSelect('name') 
      ->addAttributeToSelect('attribute_set_id') 
      ->addAttributeToSelect('type_id') 
      ->addAttributeToSelect('thumbnail'); 
     //... 

和第二種方法:

protected function _prepareColumns() 
{ 
    //... 
    $this->addColumn('product_image', array(
     'header' => Mage::helper('frame')->__('Thumbnail'), 
     'column_css_class' => 'vertical-align-middle', 
     'width'  => '90px', 
     'index'  => 'frame_left', 
     'type'  => 'image', 
     'escape' => true, 
     'sortable' => false, 
     'filter' => false, 
     'renderer' => Mage::getBlockSingleton('xxx_adminhtml_block_catalog_product_grid_renderer_image') 
    )); 
    //... 

把product_image列放到你想要的位置,添加列的順序在這裏至關重要。最後一步是創建自己的圖像渲染:

class XXX_Adminhtml_Block_Catalog_Product_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract 
{ 
    public function render(Varien_Object $row) 
    { 
     $thumbnail = $row->getThumbnail(); 
     $gridImageSrc = $this->getSkinUrl('images/np_thumb2.gif'); 
     if($thumbnail != 'no_selection') { 
      $temp = str_replace("\\","/", Mage::getBaseUrl('media') . 'catalog'. DS . 'product' . $thumbnail); 
      $fileExistsRemote = @fopen($temp, 'r'); 
      if($fileExistsRemote) { 
       $gridImageSrc = $temp; 
      } 
      @fclose($fileExistsRemote); 
     } 
     $html = '<img '; 
     $html .= 'id="' . $this->getColumn()->getId() . '" '; 
     $html .= 'width="80" '; 
     $html .= 'height="80" '; 
     $html .= 'src="' . $gridImageSrc . '" '; 
     $html .= 'class="grid-image vertical-align-middle"/>'; 

     return $html; 
    } 
} 

你可以伊斯利添加窗口小部件類示例代碼可能不是完美的,但應該作品類似的方式。請享用。

相關問題