我正在尝试通过将产品划分为子类别来组织类别页面。当我使用代码:
遍历子类别(在catalog \ product \ list.phtml上)时$ child_cat = Mage :: getModel('catalog / category') - > load($ child_id); $ _productCollection = Mage :: getResourceModel('catalog / product_collection') - > addCategoryFilter($ child_cat);
然后继续循环浏览产品......
foreach($ _productCollection as $ _product):
似乎没有区分设置为可见的产品和不显示的产品(均显示)。它也不显示图像,价格或任何其他信息。我得到的唯一正确信息是产品网址。
为什么会发生这种情况,我该如何解决?
答案 0 :(得分:3)
默认情况下,当您加载产品集合时,您将获得有关产品的大量信息。例如:
Array
(
[entity_id] => 9
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-07 16:04:58
[updated_at] => 2012-12-11 16:21:37
[cat_index_position] => 0
[stock_item (Varien_Object)] => Array
(
)
)
您需要明确告诉Magento加载额外信息或按以下值过滤:
$_productCollection = Mage::getResourceModel( 'catalog/product_collection' );
// Filter by enabled products
$_productCollection->addAttributeToFilter( 'status', 1 );
// Load all product information
$_productCollection->addAttributeToSelect( '*' );
$_productCollection->addCategoryFilter( $category );
现在您将看到类似的内容(使用$_product->debug()
转储一些信息):
Array
(
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-05 18:47:39
[updated_at] => 2012-12-11 16:20:25
[cat_index_position] => 0
[status] => 1
[visibility] => 4
[enable_googlecheckout] => 1
[tax_class_id] => 2
[is_recurring] => 0
[weight] => 3.0000
[price] => 534.2500
[name] => Sample Product
[url_key] => some-product
[is_returnable] => 2
[msrp_enabled] => 2
[msrp_display_actual_price_type] => 4
[image] => /w/r/something.png
[small_image] => /w/r/something_sm.png
[thumbnail] => /w/r/something_th.png
[options_container] => container2
[url_path] => some-product.html
[image_label] => One image label
[small_image_label] => Another image label
[thumbnail_label] => An image label
[description] => Long winded blah, blah, blah.
[short_description] => Blah, blah, blah.
[stock_item (Varien_Object)] => Array
(
)
)
媒体库信息(标签等)是一种不同的野兽,必须通过getMediaGalleryImages()
对象的Mage_Catalog_Model_Product
方法专门请求。
HOWEVER 如果在循环浏览产品集合时调用此方法将返回NULL
。奇怪的是,如果没有明确加载产品模型,您将无法访问此数据(原因我不会在此响应中介绍)。所以,我们需要尝试这样的事情:
$product = Mage::getModel( 'catalog/product' )->load( $_product->getId() );
$images = $product->getMediaGalleryImages();
虽然a performance hit在你的收集循环中这样做,所以要小心。
编辑:
Mage_Catalog_Block_Product-> getLoadedProductCollection()
长,长,<强>长故事短(这种方法深入挖掘)......据我所知getLoadedProductCollection()
只会显示Used In Product Listing
设置为的属性Yes
。
原因在于Mage_Catalog_Model_Resource_Config ......
public function getAttributesUsedInListing()
{
// ... some code above omitted...
->where('main_table.entity_type_id = ?', (int)$this->getEntityTypeId())
// THIS RIGHT HERE IS WHY
->where('additional_table.used_in_product_listing = ?', 1);
return $adapter->fetchAll($select);
}