0

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:

  1. Get all products from Set 2? or
  2. Get product name & URL from Set 1?

p.s. all items have:

  • Status = Enable
  • Visibility = Catalog, Search
  • Price > 0
  • Have stock left

1 Answer 1

1

Set 1, retrieves all products assigned to the category even if they are disabled or they not visible.
but you don't get the name or description because you don't have the ->addAttributeToSelect('*') set in the collection. Add the line right after getCollection().

In addition you can specify this also to the collection

->addUrlRewrite($category->getId())

to get the correct urls including the category path.

To get all products from set 2, you need to remove the filter on the status and visibility.

4
  • Thanks for the reply. But all the 9 products are enabled & visible. Quite strange.
    – Raptor
    Aug 11, 2015 at 9:08
  • this is strange, because getProductCollection from the category model actually calls addCategoryFilter from the product collection model. So it should be the same thing. Are you sure you don't have a limit set somewhere?
    – Marius
    Aug 11, 2015 at 9:11
  • After removing ->addAttributeToFilter('status', 1) and ->addAttributeToFilter('visibility', 4), there are still only 4 products showing. Quite strange indeed.
    – Raptor
    Aug 11, 2015 at 9:20
  • impicit ->setPage() ? Aug 11, 2015 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.