【问题标题】:Adding custom column into configurable product grid in admin在管理员中将自定义列添加到可配置的产品网格中
【发布时间】:2015-12-06 19:23:52
【问题描述】:

我为关联产品创建了一个自定义列(基本上每个简单的产品都会在可配置产品的“关联产品”选项卡下拥有该列)。我将此字段/列的信息存储到属于可配置产品而不是每个简单产品的属性中。

下面是添加列的代码;

config.xml

<core_block_abstract_to_html_before>
     <observers>
        <something>
          <class>namespace_JsonProductInfo_Model_Observer</class>
          <method>addPositionColumn</method>
        </something>
     </observers>
</core_block_abstract_to_html_before>

Oberver.php

public function addPositionColumn(Varien_Event_Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid) {
            if (!$block->isReadonly()) {
                $block->addColumnAfter(
                    'position_simple',
                    array(
                        'header'     => Mage::helper('jsonproductinfo')->__('Position'),
                        'type'      => 'input',
                        'name'      => 'position_simple',
                        'index'     => 'position_simple',
                        'sortable'  => false,
                    ),
                    'name'
                );
            }
        }
    }

使用上面的代码,我可以将列添加到需要网格中。而且我还能够保存我放在该列中的信息(这基本上是一个属性)。我面临的唯一问题是,我无法再次将存储的信息显示到该列中(如果我编辑产品)。我还创建了另一个 eav_collection_abstract_load_befor 类型的观察者,但到目前为止还没有运气。 下面是它的代码:

config.xml

<eav_collection_abstract_load_before>
  <observers>
    <jsonproductinfo>
      <class>namespace_JsonProductInfo_Model_Observer</class>
      <method>addPositionToCatalogProductCollection</method>
    </jsonproductinfo>
  </observers>
</eav_collection_abstract_load_before>

Observer.php

public function addPositionToCatalogProductCollection($observer)
    {
        $collection = $observer->getEvent()->getCollection();
        if (!isset($collection)) {
            return;
        }
        $collection->addAttributeToSelect('position_simple');
    }

如果有人能指出错误或指导我如何在该列中再次显示该信息,我将不胜感激。

编辑这里是创建“position_simple”属性的代码。

$this->addAttribute(
    'catalog_product',
    'position_simple',
    array(
        'group'             => 'General',
        'type'              => 'varchar',
        'input'             => 'hidden',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Simple Position',
        'class'             => '',
        'source'            => '',
        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'           => true,
        'required'          => false,
        'user_defined'      => true,
        'default'           => '',
        'searchable'        => false,
        'filterable'        => false,
        'comparable'        => false,
        'visible_on_front'   => false,
        'visible_in_advanced_search'   => false,
        'unique'            => false,
        'apply_to'          => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
        'is_configurable'   => false,
    )
);

【问题讨论】:

    标签: magento magento-1.9 configurable-product


    【解决方案1】:
    1. 在您的 Observer.php(函数 addPositionColumn)中将 'index' =&gt; 'position_simple' 更改为 'index' =&gt; 'entity_id'
    2. 您需要从可配置产品中获取 position_simple 属性的值,并在添加的列中使用它 - 一种方法:

    Observer.php

    中创建函数
    protected function _getDefaultConfigurationId() {
        /** @var Mage_Catalog_Model_Product $product */
        $product = Mage::registry('current_product');
        if ($product) {
            return array($product->getData('position_simple'));
        }
        return '';
    }
    

    并像这样在 addPositionColumn 函数中使用它:

    ...    
    'values' => $this->_getDefaultConfigurationId(),
    ...
    

    之后,就完全不需要观察者类型eav_collection_abstract_load_befor了。

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多