[英]Create magento 1 module to add column image and column category to product management table
I'm just a beginer in magento and have a task adding column image and column category to product management table (admin section). 我只是magento的初学者,并且有一个向产品管理表(管理部分)添加列图像和列类别的任务。 Can anyone tell me the workflow (just which steps I have to follow)?
谁能告诉我工作流程(仅我必须遵循的步骤)?
I'm using magento 1.9.2.4. 我正在使用magento 1.9.2.4。
First of all, to add new column to existing product management table you must extend magento block: Mage_Adminhtml_Block_Catalog_Product_Grid. 首先,要将新列添加到现有产品管理表中,必须扩展magento块:Mage_Adminhtml_Block_Catalog_Product_Grid。 To do so, you could create your custom module fe.
为此,您可以创建自定义模块fe。 named XXX and in your config.xml file put these lines:
名为XXX并在您的config.xml文件中放入以下行:
<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>XXX_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
Now in your file XXX_Adminhtml_Block_Catalog_Product_Grid you need to overwrite two methods: _prepareCollection() 现在,在文件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');
//...
and the second method: 第二种方法:
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')
));
//...
Put product_image column as you want, the order of adding column is crucial here. 根据需要放置product_image列,此处添加列的顺序至关重要。 The last step is to create your image renderer:
最后一步是创建图像渲染器:
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;
}
}
Similar way you can easly add category widget Sample code may not being perfect but should works. 您可以轻松地添加类别窗口小部件的类似方法示例代码可能并不完美,但应该可以。 Enjoy.
请享用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.