0

I want to get ONE random product from the productlist of a category. The product must have "status=1".

Is it possible?

Thx.

3
  • every thing is possible my friend
    – Keyul Shah
    Jun 19, 2014 at 12:20
  • by query twice, list and get random of it. then get product.
    – Tai
    Jun 19, 2014 at 12:23
  • You need to extend the API, just search for Magento Custom API and you will come across various resources explaining how to do it.
    – user2935
    Jun 19, 2014 at 12:23

2 Answers 2

2

You need to refine your product with status attribute filter.

$mydata['id'] it specified the category id's array.

$collection = Mage::getModel('catalog/product')
                             ->getCollection()
                             ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                             ->addAttributeToSelect('*')
                             ->addAttributeToFilter('category_id', array('eq' =>$mydata['id'][$key]))
->addAttributeToFilter('status','1')
                         ->getData();
0

I think It is Proper way for you.

$category = Mage::getModel('catalog/category')->load($catId);

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*') // select all attributes
    ->addCategoryFilter($category)
->addStoreFilter($storeId);

$collection->getSelect()->order('rand()');

$collection->setPageSize(1) // limit number of results returned
    ->setCurPage(0)
    ->load()
    ;

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.