0

我正在尝试通过将产品划分为子类别来组织类别页面。当我使用代码遍历子类别(在 catalog\product\list.phtml 上)时:

$child_cat = Mage::getModel('catalog/category')->load($child_id); $_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($child_cat);

然后继续循环浏览产品...

foreach ($_productCollection 作为 $_product):

它似乎没有区分设置为可见的产品和不可见的产品(都显示)。它也不显示图像、价格或任何其他信息。我得到的唯一正确信息是产品 URL。

为什么会发生这种情况,我该如何解决?

4

1 回答 1

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

但是NULL,如果在循环通过产品集合时调用此方法将返回。奇怪的是,如果不明确加载产品模型,您将无法访问这些数据(原因我不会在此回复中详细说明)。所以,我们需要尝试这样的事情:

$product = Mage::getModel( 'catalog/product' )->load( $_product->getId() );
$images = $product->getMediaGalleryImages();

但是,在您的收集循环中执行此操作会影响性能,所以要小心。


编辑:

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);
}
于 2013-01-07T16:08:32.403 回答