I want to get ONE random product from the productlist of a category. The product must have "status=1".
Is it possible?
Thx.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want to get ONE random product from the productlist of a category. The product must have "status=1".
Is it possible?
Thx.
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();
$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()
;