YWSW
YWSW

Reputation: 453

magento category loop not showing important product information

I am trying to organize the category page by dividing the products into subcategories. When I loop through the subcategories (on catalog\product\list.phtml) with the code:

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

and then proceed to loop through the products...

foreach ($_productCollection as $_product):

It does not seem to differentiate between products that are set to be visible and products that are not (both are shown). It also does not show images, prices, or any other information. The only correct information I get is the product URL.

Why is this happening and how do I fix it?

Upvotes: 0

Views: 738

Answers (1)

CVEEP
CVEEP

Reputation: 441

By default, when you load a product collection you'll get a bare amount of information about the products. Ex:

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

)

You need to explicitly tell Magento to load extra information or to filter by a value like this:

$_productCollection = Mage::getResourceModel( 'catalog/product_collection' );
// Filter by enabled products
$_productCollection->addAttributeToFilter( 'status', 1 );
// Load all product information
$_productCollection->addAttributeToSelect( '*' );
$_productCollection->addCategoryFilter( $category );

Now you'll see something like this (using $_product->debug() to dump out some information):

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

)

The media gallery information (labels, etc) is a different beast and must be specifically requested through the getMediaGalleryImages() method of the Mage_Catalog_Model_Product object.

HOWEVER this method will return NULL if called while looping through a product collection. Oddly, you cannot access this data without explicitly loading the product model (for reasons I won't go into in this response). So, we need to try something like this:

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

There's a performance hit doing this in your collection loop though, so be careful.


EDIT:

Mage_Catalog_Block_Product->getLoadedProductCollection()

Long, long, long story short (this method digs deep)... as far as I can tell getLoadedProductCollection() will only show attributes which have Used In Product Listing set to Yes.

enter image description here

The reason resides in 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);
}

Upvotes: 3

Related Questions