繁体   English   中英

如何在magento 1.9.2社区版中的产品网格上添加自定义属性

[英]How to Add Custom Attribute on product Grid in magento 1.9.2 community edition

我在将自定义属性显示为网格中的列时出现问题。

首先,我使用更新脚本以编程方式创建了一个属性,这里是代码:

$installer = $this;

$installer->startSetup();

// Set data:
$attributeName  = 'Owners'; // Name of the attribute
$attributeCode  = 'owners'; // Code of the attribute
$attributeGroup = 'General';          // Group to add the attribute to
$attributeSetIds = array(4);          // Array with attribute set ID's to add this attribute to. (ID:4 is the Default Attribute Set)

// Configuration:
$data = array(
'type'      => 'varchar',       // Attribute type
'input'     => 'text',          // Input type
'global'    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,    // Attribute scope
'required'  => false,           // Is this attribute required?
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'used_in_product_listing' => true,
'default_value_yesno' => '0',
// Filled from above:
'label' => $attributeName
);

// Create attribute:
// We create a new installer class here so we can also use this snippet in a non-EAV setup script.
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup'); 

$installer->addAttribute('catalog_product', $attributeCode, $data);


$installer->addAttributeToSet(
'catalog_product', 'Default', $attributeGroup, $attributeCode
); //Default = attribute set, General = attribute group



$installer->endSetup();

这段代码对我来说很好,它正在创建一个属性。

比我成功覆盖grid.php并将我的代码放入其中但它不工作列在网格中可见,但数据不会到来。

grid.php的代码

public function __construct()
{
    parent::__construct();
    $this->setId('productGrid');
    $this->setDefaultSort('entity_id');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
    $this->setVarNameFilter('product_filter');

}

protected function _getStore()
{
    $storeId = (int) $this->getRequest()->getParam('store', 0);
    return Mage::app()->getStore($storeId);
}

protected function _prepareCollection()
{
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id')
        ->addAttributeToSelect('owners');

    if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
        $collection->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');
    }
    if ($store->getId()) {
        //$collection->setStoreId($store->getId());
        $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
        $collection->addStoreFilter($store);
        $collection->joinAttribute(
            'name',
            'catalog_product/name',
            'entity_id',
            null,
            'inner',
            $adminStore
        );
        $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->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    }

    $this->setCollection($collection);

    parent::_prepareCollection();
    $this->getCollection()->addWebsiteNamesToResult();
    return $this;
}

protected function _addColumnFilterToCollection($column)
{
    if ($this->getCollection()) {
        if ($column->getId() == 'websites') {
            $this->getCollection()->joinField('websites',
                'catalog/product_website',
                'website_id',
                'product_id=entity_id',
                null,
                'left');
        }
    }
    return parent::_addColumnFilterToCollection($column);
}

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',
    ));

    $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',
    ));

    if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
        $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(),
    ));

    $this->addColumn('owners',
        array(
            'header'=> Mage::helper('catalog')->__('Owner'),
            'width'=>'200px',
            'index' => 'owners',
        ));
    return parent::_prepareColumns();

    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->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',
    ));

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

    return parent::_prepareColumns();
}

protected function _prepareMassaction()
{
    $this->setMassactionIdField('entity_id');
    $this->getMassactionBlock()->setFormFieldName('product');

    $this->getMassactionBlock()->addItem('delete', array(
         'label'=> Mage::helper('catalog')->__('Delete'),
         'url'  => $this->getUrl('*/*/massDelete'),
         'confirm' => Mage::helper('catalog')->__('Are you sure?')
    ));

    $statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();

    array_unshift($statuses, array('label'=>'', 'value'=>''));
    $this->getMassactionBlock()->addItem('status', array(
         'label'=> Mage::helper('catalog')->__('Change status'),
         'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
         'additional' => array(
                'visibility' => array(
                     'name' => 'status',
                     'type' => 'select',
                     'class' => 'required-entry',
                     'label' => Mage::helper('catalog')->__('Status'),
                     'values' => $statuses
                 )
         )
    ));

    if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
        $this->getMassactionBlock()->addItem('attributes', array(
            'label' => Mage::helper('catalog')->__('Update Attributes'),
            'url'   => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
        ));
    }

    Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
    return $this;
}

public function getGridUrl()
{
    return $this->getUrl('*/*/grid', array('_current'=>true));
}

public function getRowUrl($row)
{
    return $this->getUrl('*/*/edit', array(
        'store'=>$this->getRequest()->getParam('store'),
        'id'=>$row->getId())
    );
}

在此输入图像描述

@Raphael在数字钢琴家的作品

是的,这是一个常见的问题。

所以这里的问题是,随着您的自定义网格扩展原始网格类

Mage_Adminhtml_Block_Catalog_Product_Grid, 

当您调用以下代码时:

return parent::_prepareColumns();

它转到原始的_prepareColumns方法,因此会覆盖您的自定义列。

要解决此问题,您需要调用父级的父方法:

return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();

_prepareCollection方法也是如此。

为了避免这种问题,我还建议您使用事件观察器而不是重写块: https ://magento.stackexchange.com/a/5986/2380

您错过了渲染器,因为它无法加载数据。 尝试这个:

    $this->addColumn('owners',
     array(
        'header'=> Mage::helper('catalog')->__('Owner'),
        'width'=>'200px',
        'index' => 'owners',
        'renderer' => 'adminhtml/widget_grid_column_renderer_owner'
    ));

根据您的要求更改渲染器路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM