【问题标题】:magento category loop not showing important product informationmagento 类别循环未显示重要的产品信息
【发布时间】:2012-12-21 08:26:25
【问题描述】:

我正在尝试通过将产品划分为子类别来组织类别页面。当我使用代码循环遍历子类别(在 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。

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

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    默认情况下,当您加载产品集合时,您将获得有关产品的少量信息。例如:

    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
            (
            )
    
    )
    

    媒体库信息(标签等)是另一回事,必须通过Mage_Catalog_Model_Product 对象的getMediaGalleryImages() 方法专门请求。

    但是如果在循环通过产品集合时调用此方法,将返回 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);
    }
    

    【讨论】:

    • 哇 - 感谢您的详细回复。您警告过性能受到影响 - 我对此非常担心。所有产品信息都是通过 $_productCollection=$this->getLoadedProductCollection(); 在页面开头加载的,目前我正在抓取子类别并尝试获取产品的另一个完整信息加载(你在这里回答的问题)。这似乎是重复的 - 有没有办法避免这种情况?
    • 性能损失来自(技术上)在 foreach 循环中重新加载/查询每个产品模型。不理想。简短的回答是:这取决于您的设置。使用产品模型的->debug() 方法查看在您简单地遍历产品集合时可用的内容。查看您可以使用的内容并通过addAttributeToSelect 慢慢添加您需要的字段。当您指定 addAttributeToSelect( 'images' ) 时,看起来图库基本信息已包含在内,因此您可以继续使用。
    • 我会调查的 - 非常感谢你的时间和知识 - 你对我帮助很大!
    • 啊,我刚刚注意到$this->getLoadedProductCollection() ...是的,这很有趣。除了在某些自定义代码中覆盖该方法之外,您无法修改它,但是,IIRC,它变得有点痛苦。我会在下面进入它。
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多