我正在优化magento商店,我遇到了几篇文章,建议对有大量SKU的商店使用扁平产品目录。
由于我有超过1万的产品,我想我应该试一试。但是,当使用平面产品目录时,产品对象中只加载少数几个属性(例如SKU、名称、简短描述)。我的模板在搜索/浏览视图中显示了一些其他属性,如制造商和颜色。
有没有办法将这些属性添加到平面产品目录表中,以便也可以访问它们?
1.4.x.x,只需进入要在"平面产品目录"中使用的属性,并确保属性在产品列表中使用设置为是。更改后,重新索引"扁平产品数据"
以下属性导致属性包含在"平面产品目录"中:
"Use in Layered Navigation" = Yes
"Used in Product Listing" = Yes
"Used for Sorting in Product Listing" = Yes
我也一直在处理这个问题,我将其描述为"无法在平面模式下访问产品集合属性"或"addAttributeToSelect无法在平面方式下工作"。
我找到了一个"干净"的解决方案:
- 不要求属性在admin中具有特定设置(它可能由用户添加,或隐藏在前端)
- 适用于平面和非平面模式
请注意,在下面的代码中,我使用了相关的产品集合,但这适用于任何产品集合(特别是从Mage_Eav_Model_Entity_Collection_Abstract
继承的任何东西)
失败代码:
$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
->addAttributeToSelect( 'my_custom_attribute' )
;
在平面模式下,如果属性恰好不在平面表中,则上面的代码将无法添加该属性。
工作代码:
$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
->joinAttribute( 'my_custom_attribute', 'catalog_product/my_custom_attribute', 'entity_id', null, 'left' )
->addAttributeToSelect( 'my_custom_attribute' )
;
joinAttribute
方法向查询添加联接。即使这复制了平面表中已经存在的属性,它也能工作。
请注意,我在那里使用了left
联接,以确保如果没有在这些产品上设置my_custom_attribute
,它会获取产品。如果您只对设置了my_custom_attribute
的行感兴趣,则可以更改inner
的设置。
(在CE 1.6.2.0中测试)
其他人提供了通常足够的答案(将Used In Product Listing设置为Yes),但我发现了一个边缘案例,当我搜索相同的东西时,它会很有用。
如果要将自定义源模型用于要包含在平面产品表中的自定义属性,则必须覆盖源模型类中的getFlatColums()
。是的,我知道它拼错了,但对你来说那是土生土长的马根托。
样本:
public function getFlatColums() {
return array($this->getAttribute()->getAttributeCode() => array(
'type' => 'tinyint',
'unsigned' => true,
'is_null' => true,
'default' => null,
'extra' => null
));
}
来源:http://www.dconstructing.com/2012/03/14/custom-product-attributes-and-flat-database-tables-in-magento
如果您想在使用平面产品表时使用模块中的属性(例如过滤产品集合),可以将其添加到模块的module_File.xml
<?xml version="1.0"?>
<config>
<modules>
<Your_Module>
<active>true</active>
<codePool>local</codePool>
</Your_Module>
</modules>
<frontend>
<product>
<collection>
<attributes>
<your_custom_attribute /> <!-- This is the attribute name -->
</attributes>
</collection>
</product>
</frontend>
</config>