Given a $product
, I need to get a random collection of 10 products belonging to the same categories of $product
with a similar price - let's say +-15% the price of $product
.
My idea is to get all product's $categories
, then load a $cat_products
collection for each category, then loop over $cat_products
and check each $cat_product
against my price condition and eventually add the $cat_product
to an array if it matches. Then return the array.
$categoryIds = $product->getResource()->getCategoryIds($product);
foreach ($categoryIds as $categoryId) {
$cat_products = Mage::getModel('catalog/category')->load($categoryId)
->getProductCollection()
->addAttributeToFilter('status', 1)
foreach ($cat_products as $cat_product) {
// check against my price conditions
}
}
I didn't try it yet, but it should work. It looks extremely inefficient, though (also, I have a large catalog with many categories, and many categories - especially roots categories - are shared among products, so I would end up loading a few hundred thousands of products ...).
Any idea how can I get same result in a more efficient way?