+ -
当前位置:首页 → 问答吧 → 在magento的product列表中加入自定义字段

在magento的product列表中加入自定义字段

时间:2009-08-25

来源:互联网

好久不来没权限发帖子,终于给权限了。庆祝一下。
我其实是expert ezpublish,不过最近做magento,就写点心得。

其实是想发个帖子,看看我在什么组里面能有权限发帖子了

闲话不说,首先用module creator建立一个自己的extension。
我比较懒,所以都是用他来建立extension。之后删去不用的文件。
这一步祥见 http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table
比如建立的extension是Lingping, Module是Projet
这样 他就给你建立好了 app/code/local/Lingping/Projet 的目录

比如我们要在product得后台list里面加上soustitre这个栏位

在里面建立
app/code/local/Lingping/Projet/Adminhtml/Block/Catalog/Product/Grid.php

我用firefox,怎么编辑器的按钮没有用!!!是不是太慢了,就没用了。我用别的discuz论坛都是好的。那就麻烦班主编辑了
<?php
Class Lingping_Projet_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
       
         protected function _prepareCollection()
    {
        $store = $this->_getStore();
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('soustitre')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('attribute_set_id')
            ->addAttributeToSelect('type_id')
            ->joinField('qty',
                'cataloginventory/stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left');

        if ($store->getId()) {
            //$collection->setStoreId($store->getId());
            $collection->addStoreFilter($store);
            $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
            $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId());
            $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
            $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
        }
        else {
            $collection->addAttributeToSelect('price');
            $collection->addAttributeToSelect('status');
            $collection->addAttributeToSelect('visibility');
        }

        $this->setCollection($collection);

        Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
        $this->getCollection()->addWebsiteNamesToResult();
        return $this;
    }
        protected function _prepareColumns()
    {
               $this->addColumn('entity_id',
            array(
                'header'=> Mage::helper('catalog')->__('ID'),
                'width' => '50px',
                'type'  => 'number',
                'index' => 'entity_id',
        ));
        $this->addColumn('name',
            array(
                'header'=> Mage::helper('catalog')->__('Name'),
                'index' => 'name',
        ));
        $this->addColumn('soustitre',
            array(
                'header'=> Mage::helper('catalog')->__('Collection'),
                'width' => '80px',
                'index' => 'soustitre',
        ));
        $store = $this->_getStore();
        if ($store->getId()) {
            $this->addColumn('custom_name',
                array(
                    'header'=> Mage::helper('catalog')->__('Name In %s', $store->getName()),
                    'index' => 'custom_name',
            ));
        }

        $this->addColumn('type',
            array(
                'header'=> Mage::helper('catalog')->__('Type'),
                'width' => '60px',
                'index' => 'type_id',
                'type'  => 'options',
                'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
        ));

        $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->load()
            ->toOptionHash();

        $this->addColumn('set_name',
            array(
                'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
                'width' => '100px',
                'index' => 'attribute_set_id',
                'type'  => 'options',
                'options' => $sets,
        ));

        $this->addColumn('sku',
            array(
                'header'=> Mage::helper('catalog')->__('SKU'),
                'width' => '80px',
                'index' => 'sku',
        ));

        $store = $this->_getStore();
        $this->addColumn('price',
            array(
                'header'=> Mage::helper('catalog')->__('Price'),
                'type'  => 'price',
                'currency_code' => $store->getBaseCurrency()->getCode(),
                'index' => 'price',
        ));

        $this->addColumn('qty',
            array(
                'header'=> Mage::helper('catalog')->__('Qty'),
                'width' => '100px',
                'type'  => 'number',
                'index' => 'qty',
        ));

        $this->addColumn('visibility',
            array(
                'header'=> Mage::helper('catalog')->__('Visibility'),
                'width' => '70px',
                'index' => 'visibility',
                'type'  => 'options',
                'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
        ));

        $this->addColumn('status',
            array(
                'header'=> Mage::helper('catalog')->__('Status'),
                'width' => '70px',
                'index' => 'status',
                'type'  => 'options',
                'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('websites',
                array(
                    'header'=> Mage::helper('catalog')->__('Websites'),
                    'width' => '100px',
                    'sortable'  => false,
                    'index'     => 'websites',
                    'type'      => 'options',
                    'options'   => Mage::getModel('core/website')->getCollection()->toOptionHash(),
            ));
        }


        $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
        

        $this->addColumn('action',
            array(
                'header'    => Mage::helper('catalog')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('catalog')->__('Edit'),
                        'url'     => array(
                            'base'=>'*/*/edit',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
        ));
        
        return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
    }
}
?>
在config.xml里<global>里面加上
<blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>LingPing_Projet_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                   </rewrite>
            </adminhtml>
        </blocks>

大家再看看~就好了

我想传个做好的附件,不行,我发表了在试验一下吧

作者: 伶俜   发布时间: 2009-08-25

哇,看到了,竟然是贵宾待遇。谢谢阿~~~

我上个照片

magento.jpg (146.28 KB)

下载次数:0

2009-8-25 21:51

作者: 伶俜   发布时间: 2009-08-25

就是那个collection得栏位,,就是我上面说的soustitre

希望对大家有点用

作者: 伶俜   发布时间: 2009-08-25

你好 请问一下
                $collectionProduct =Mage::getModel('catalog/product')->getCollection();

                $collectionProduct->addAttributeToSelect('*');
这个collection里面调用 $collection->getMediaGalleryImages() 为什么取不到catalog_product_entity_media_gallery 这个表格里面的value数据 ?

作者: r2958   发布时间: 2009-09-01

hi,对gallery的调用,如下
$_gallery = Mage::getModel('catalog/product')->load($current_product_id)->getMediaGalleryImages();

作者: 伶俜   发布时间: 2009-09-16