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 Commented Jun 19, 2014 at 12:20
  • by query twice, list and get random of it. then get product. Commented 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. Commented 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 you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.