I have 2 sets of codes to get products in category.
Set 1
$category = Mage::getModel('catalog/category')->load(4);
$products = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->load();
Set 2
$products = Mage::getModel('catalog/category')->load(4)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->setOrder('price', 'ASC'); //sets the order by price
With Set 1, I can obtain correct number of products (9 items in total), while in Set 2, I can only obtain 4 items (strange).
However, only Set 2 returns product model (and I can obtain information such as names from it). Set 1 returns the following object:
Mage_Catalog_Model_Product::__set_state(array(
'_cacheTag' => 'catalog_product',
'_eventPrefix' => 'catalog_product',
'_eventObject' => 'product',
'_canAffectOptions' => false,
'_typeInstance' => NULL,
'_typeInstanceSingleton' => NULL,
'_linkInstance' => NULL,
'_customOptions' =>
array (
),
'_urlModel' => NULL,
'_errors' =>
array (
),
'_optionInstance' => NULL,
'_options' =>
array (
),
'_reservedAttributes' => NULL,
'_isDuplicable' => true,
'_calculatePrice' => true,
'_defaultValues' =>
array (
),
'_storeValuesFlags' =>
array (
),
'_lockedAttributes' =>
array (
),
'_isDeleteable' => true,
'_isReadonly' => false,
'_resourceName' => 'catalog/product',
'_resource' => NULL,
'_resourceCollectionName' => 'catalog/product_collection',
'_dataSaveAllowed' => true,
'_isObjectNew' => NULL,
'_data' =>
array (
'entity_id' => '9',
'entity_type_id' => '4',
'attribute_set_id' => '4',
'type_id' => 'simple',
'sku' => 'SKU101',
'has_options' => '0',
'required_options' => '0',
'created_at' => '2015-01-19 10:29:08',
'updated_at' => '2015-07-20 08:09:53',
'cat_index_position' => '1',
'is_salable' => '1',
'stock_item' =>
Varien_Object::__set_state(array(
'_data' =>
array (
'is_in_stock' => '1',
),
'_hasDataChanges' => false,
'_origData' => NULL,
'_idFieldName' => NULL,
'_isDeleted' => false,
'_oldFieldsMap' =>
array (
),
'_syncFieldsMap' =>
array (
),
)),
),
'_hasDataChanges' => true,
'_origData' => NULL,
'_idFieldName' => 'entity_id',
'_isDeleted' => false,
'_oldFieldsMap' =>
array (
),
'_syncFieldsMap' =>
array (
),
))
How can I:
- Get all products from Set 2? or
- Get product name & URL from Set 1?
p.s. all items have:
- Status = Enable
- Visibility = Catalog, Search
- Price > 0
- Have stock left