Magento在分层导航中使用静态属性

时间:2012-10-18 08:02:54

标签: magento entity-attribute-value custom-attribute

我想知道是否有人曾经实现过新的产品属性,将其设置为类型STATIC并将渲染器输入为SELECT。该属性的范围是GLOBAL。

基本上每个产品的属性值将保存在主实体的catalog_product_entity表中。

在此设置之后,我疯狂地导入了产品。一切都运作正常。

访问前端的类别页面时,此自定义实现的属性未在图层状态中呈现,并且在分层类别导航上查看。 我设法通过改变 Mage_Catalog_Model_Resource_Layer_Filter_Attribute 类函数来解决这个问题: applyFilterToCollection getCount

此时一切都在前端和管理员上正常工作。

如果有人试过这种方法,请告诉我。我可以在这里分享我的代码,如果你有兴趣请告诉我!

添加属性的代码(在安装程序中使用):

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$l_oEAVSetup                = new Mage_Eav_Model_Entity_Setup('eav_setup');
$l_aCatalogProductEntity    = $l_oEAVSetup->getEntityType('catalog_product');
$l_iProductEntityTypeId     = $l_aCatalogProductEntity['entity_type_id'];

$installer->run("

ALTER TABLE  {$this->getTable('catalog/product')} ADD  `game_genre` INT( 11 ) UNSIGNED NULL DEFAULT NULL ,
ADD INDEX (  `game_genre` );

");

$l_oEAVSetup->addAttribute($l_iProductEntityTypeId, 'game_genre', array(
    'group'                 => 'General',
    'input'                 => 'select',
    'label'                 => 'Genre',
    'required'              => true,
    'user_defined'          => false,
    'type'                  => 'static'
));
$installer->endSetup();

Mage_Catalog_Model_Resource_Layer_Filter_Attribute

更改代码
public function applyFilterToCollection($filter, $value)
    {
        $collection = $filter->getLayer()->getProductCollection();
        $attribute  = $filter->getAttributeModel();
        $connection = $this->_getReadAdapter();
        $tableAlias = $attribute->getAttributeCode() . '_idx';
        $conditions = array(
            "{$tableAlias}.entity_id = e.entity_id",
            $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
            $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
            $connection->quoteInto("{$tableAlias}.value = ?", $value)
        );

        if ('static' === $attribute->getBackendType()) {            
            $collection->getSelect()->join(
                array($tableAlias => $this->getTable('catalog/product')),
                implode(' AND ', array(
                    $connection->quoteInto($tableAlias. '.'. $attribute->getAttributeCode(). ' = ?', $value),
                    $tableAlias. '.entity_id = e.entity_id'
                )),
                array()
            );

            //$select = $collection->getSelect();
            //$select->where('e.'. $attribute->getAttributeCode(). ' = ?', $value);         
        }
        else {

            $collection->getSelect()->join(
                array($tableAlias => $this->getMainTable()),
                implode(' AND ', $conditions),
                array()
            );
        }

        return $this;
    }

public function getCount($filter)
    {
        // clone select from collection with filters
        $select = clone $filter->getLayer()->getProductCollection()->getSelect();
        // reset columns, order and limitation conditions
        $select->reset(Zend_Db_Select::COLUMNS);
        $select->reset(Zend_Db_Select::ORDER);
        $select->reset(Zend_Db_Select::LIMIT_COUNT);
        $select->reset(Zend_Db_Select::LIMIT_OFFSET);

        $connection = $this->_getReadAdapter();
        $attribute  = $filter->getAttributeModel();
        $tableAlias = sprintf('%s_idx', $attribute->getAttributeCode());
        $conditions = array(
            "{$tableAlias}.entity_id = e.entity_id",
            $connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
            $connection->quoteInto("{$tableAlias}.store_id = ?", $filter->getStoreId()),
        );

        if ('static' === $attribute->getBackendType()) {
            $select->columns('e.'. $attribute->getAttributeCode());
            $select->columns(array('count' => new Zend_Db_Expr('COUNT(e.entity_id)')));
            $select->group('e.'. $attribute->getAttributeCode());
        }
        else {

            $select
                ->join(
                    array($tableAlias => $this->getMainTable()),
                    join(' AND ', $conditions),
                    array('value', 'count' => new Zend_Db_Expr("COUNT({$tableAlias}.entity_id)")))
                ->group("{$tableAlias}.value");
        }

        return $connection->fetchPairs($select);
    }

0 个答案:

没有答案