我正在尝试将类别列添加到产品网格中。
我修改了Mage_Adminhtml_Block_Catalog_Product_Grid
。
将以下内容添加到_prepareCollection
:
->joinField('category_ids',
'catalog/category_product_index',
'category_id',
'product_id=entity_id',
null,
'left')
这给了我一个错误:
a:5:{i:0;s:72:"Item (Mage_Catalog_Model_Product) with the same id "16243" already exist"
。
在prepareColumns中我添加:
$this->addColumn('category_ids',
array(
'header'=> Mage::helper('catalog')->__('Categories'),
'index' => 'category_ids',
'width' => '150px'
));
如何修复查询,以免出错? 是否可以按类别名称而不是ID显示和过滤?
论坛帖子显示了类似的代码,但我无法使其与类别一起使用 http://www.magentocommerce.com/boards/viewthread/44534/
static protected $COLUMN_ID_TRADE_REFERENCES = 'ref_text';
protected function _prepareCollection()
{
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id')
->addAttributeToSelect('ref_text')
->joinTable('productreferences/reference',
'product_id=entity_id',
array('ref_text'),
null,
'left')
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left')
->addStaticField('ref_text')
->addExpressionAttributeToSelect(self::$COLUMN_ID_TRADE_REFERENCES,
'GROUP_CONCAT(ref_text SEPARATOR " ; ")',
'ref_text')
->groupByAttribute('entity_id');
答案 0 :(得分:19)
答案 1 :(得分:4)
根据上面的引用。
过滤器和可排序标志将阻止列标题可单击,并删除该列的过滤器文本框。由于我们已经做了一些繁重的解决方法来将类别列放入网格中,因此这些功能无论如何都不会起作用。我不需要它们,因此我没有考虑让它们工作有多难。
您需要做的就是粘贴下面的代码
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name');
$options = array();
foreach ($collection as $item){
if($item->getId() != ''){
$options[$item->getId()] = $item->getName();
}
}
$this->addColumn('category_ids',
array(
'header' => Mage::helper('catalog')->__('Categories'),
'index' => 'single_category_id',
'width' => '150px',
'type' => 'options',
'options' => $options
));
取代
$this->addColumn('category_ids',
array(
'header' => Mage::helper('catalog')->__('Categories'),
'index' => 'category_names',
'width' => '150px',
'filter' => false,
'sortable' => false,
));
答案 2 :(得分:1)
如果您只想添加类别(不是类别路径),并且产品只有一个类别,请将其添加到集合设置中:
$collection->joinAttribute('catname','catalog_category/name','category_ids',null,'left');
答案 3 :(得分:1)
显示您可以使用的所有类别的名称
$this->addColumn('category_ids',
array(
'header'=> Mage::helper('catalog')->__('Category'),
'type' => 'options',
'index' => 'category_ids',
'options' => $this->catOptions,
'renderer' => 'Your_Module_Block_Adminhtml_List_Cat',
));
在你的课程块中
class Your_Module_Block_Adminhtml_List_Cat extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$product = Mage::getModel('catalog/product')->load($row->getEntityId());
$cats = $product->getCategoryIds();
$allCats = '';
foreach($cats as $key => $cat)
{
$_category = Mage::getModel('catalog/category')->load($cat);
$allCats.= $_category->getName();
if($key < count($cats)-1)
$allCats.= ' ,';
}
return $allCats;
}
}
答案 4 :(得分:0)
我知道现在回答为时已晚,但最近我看到了这个问题,所以我也回答,因为它也可以帮助其他人。
参考此处:here
您必须创建一个扩展名才能在产品网格中显示类别。请创建以下文件,它将适合您:
在app/code/local/SoftProdigy/AdminGridCategoryFilter/Block/Catalog/Product/Grid/Render/Category.php
位置创建新文件并添加以下代码:
<?php
class SoftProdigy_AdminGridCategoryFilter_Block_Catalog_Product_Grid_Render_Category extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$product = Mage::getModel('catalog/product')->load($row->getEntityId());
$cats = $product->getCategoryIds();
$allCats = '';
foreach($cats as $key => $cat)
{
$_category = Mage::getModel('catalog/category')->load($cat);
$allCats.= $_category->getName();
if($key < count($cats)-1)
$allCats.= ',<br />';
}
return $allCats;
}
}
在app/code/local/SoftProdigy/AdminGridCategoryFilter/etc/config.xml
位置创建新文件并添加以下代码:
<?xml version="1.0"?>
<config>
<modules>
<SoftProdigy_AdminGridCategoryFilter>
<version>0.0.0.1</version>
</SoftProdigy_AdminGridCategoryFilter>
</modules>
<global>
<models>
<admingridcategoryfilter>
<class>SoftProdigy_AdminGridCategoryFilter_Model</class>
</admingridcategoryfilter>
</models>
<helpers>
<admingridcategoryfilter>
<class>SoftProdigy_AdminGridCategoryFilter_Helper</class>
</admingridcategoryfilter>
</helpers>
<blocks>
<admingridcategoryfilter>
<class>SoftProdigy_AdminGridCategoryFilter_Block</class>
</admingridcategoryfilter>
</blocks>
</global>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<admingridcategoryfilter>
<class>admingridcategoryfilter/observer</class>
<method>addCategoryFilterToProductGrid</method>
</admingridcategoryfilter>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
</config>
在app/code/local/SoftProdigy/AdminGridCategoryFilter/Helper/Data.php
位置创建新文件并添加以下代码:
<?php
class SoftProdigy_AdminGridCategoryFilter_Helper_Data extends Mage_Core_Helper_Abstract
{
}
在app/code/local/SoftProdigy/AdminGridCategoryFilter/Model/Observer.php
位置创建新文件并添加以下代码:
<?php
class SoftProdigy_AdminGridCategoryFilter_Model_Observer
{
public function addCategoryFilterToProductGrid(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if( ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid) ) {
$block->addColumnAfter('softprodigy_category_list', array(
'header' => Mage::helper('admingridcategoryfilter')->__('Category'),
'index' => 'softprodigy_category_list',
'sortable' => false,
'width' => '250px',
'type' => 'options',
'options' => Mage::getSingleton('admingridcategoryfilter/system_config_source_category')->toOptionArray(),
'renderer' => 'admingridcategoryfilter/catalog_product_grid_render_category',
'filter_condition_callback' => array($this, 'filterCallback'),
),'name');
}
}
public function filterCallback($collection, $column)
{
$value = $column->getFilter()->getValue();
$_category = Mage::getModel('catalog/category')->load($value);
$collection->addCategoryFilter($_category);
return $collection;
}
}
在app/code/local/SoftProdigy/AdminGridCategoryFilter/Model/System/Config/Source/Category.php
位置创建新文件并添加以下代码:
<?php
class SoftProdigy_AdminGridCategoryFilter_Model_System_Config_Source_Category
{
public function toOptionArray($addEmpty = true)
{
$options = array();
foreach ($this->load_tree() as $category) {
$options[$category['value']] = $category['label'];
}
return $options;
}
public function buildCategoriesMultiselectValues(Varien_Data_Tree_Node $node, $values, $level = 0)
{
$level++;
$values[$node->getId()]['value'] = $node->getId();
$values[$node->getId()]['label'] = str_repeat("--", $level) . $node->getName();
foreach ($node->getChildren() as $child)
{
$values = $this->buildCategoriesMultiselectValues($child, $values, $level);
}
return $values;
}
public function load_tree()
{
$store = Mage::app()->getFrontController()->getRequest()->getParam('store', 0);
$parentId = $store ? Mage::app()->getStore($store)->getRootCategoryId() : 1; // Current store root category
$tree = Mage::getResourceSingleton('catalog/category_tree')->load();
$root = $tree->getNodeById($parentId);
if($root && $root->getId() == 1)
{
$root->setName(Mage::helper('catalog')->__('Root'));
}
$collection = Mage::getModel('catalog/category')->getCollection()
->setStoreId($store)
->addAttributeToSelect('name')
->addAttributeToSelect('is_active');
$tree->addCollectionData($collection, true);
return $this->buildCategoriesMultiselectValues($root, array());
}
}
在app/etc/modules/SoftProdigy_AdminGridCategoryFilter.xml
位置创建新文件并添加以下代码:
<?xml version="1.0"?>
<config>
<modules>
<SoftProdigy_AdminGridCategoryFilter>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog />
<Mage_Adminhtml />
</depends>
</SoftProdigy_AdminGridCategoryFilter>
</modules>
</config>
现在从缓存管理中清除缓存,您可以在产品网格中看到类别列。