Keep the collection always fixed to certain number

I am using the following code to get five Best sellers collection. The number five is fixed here.

// Array of Id's to retrieve from product collection
$dataArray = array(2, 8 , 17, 3, 7);
$collection = Mage::getResourceModel('reports/product_collection')
        ->addFieldToFilter('entity_id', $dataArray)
        ->addAttributeToSelect('*');
// shuffling the retrieved collection
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
// limiting the collection to number 5
$collection->setPageSize(5);

$dataArray is a variable which holds the product ids.

In above code, the $dataArray can have less than five ids. In those cases, I am getting the collection less than five. But my requirement is that the collection must have five products even the $dataArray holds less than 5 values. I mean the remaining product collection should come from the same Bestsellers collection excluding the $dataArray values.

How should I do this?

I can do this something like this, which I think is not good for performance:

 $count = count($collection);
 if($count < 5){
     $remainingCount = 5 - $count;
     // Again run the above product collection
     // exclude the product ids which are already present in array
     // setPageSize to $remainingCount
     // and somehow merge the above specific best sellers with this 
 }

Solutions

Do the same and exclude the IDs:

$remainingCount = 5 - $count;
// Array of Id's to retrieve from product collection
$dataArray = array(2, 8 , 17, 3, 7);
$collection = Mage::getResourceModel('reports/product_collection')
        ->addFieldToFilter('entity_id', $dataArray)
        ->addAttributeToSelect('*');
// shuffling the retrieved collection
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
// limiting the collection to number 5
$collection->setPageSize($remainingCount);
// exclude the IDs
$collection->setAttributeToFilter('entity_id', array('nin' => $dataArray));

Similar questions

how to restrict category creation more than certain number in magento
My need is : If I set category limit is 50. when I try to create a 51-th category that will not be created How I do this in magento. and also, how to I set limit for products like above. any easy way available for this.
Restricting customers to max of (some number) purchases for a certain product in Magento
Is there any good modules out there that can handle this? Or would this be a custom event / observer that needs to be written? Here's an image on a non magento site that shows specifically what i'm looking for. Magento 2.2.3 EE This needs to work with the Magento commerce b2b module. So everyone in a group/company would be limited. If there was 4 p...
Magento You Save Percentage on Product Page only if saving percentage is above certain number
I would like to show You Save Percentage on Product Page only if saving percentage is above 10%. I am using this code which I found while searching google but unable to get the desired result. Please help me.
how to show certain number of products for guests in magento
I need show 10 or any number products for guest user. So how to show certain number of products for guests or special users groups in magento?
Why is size of a collection greater than the number of products in that collection?
In toolbar.phtml there is $block->getCollection()->getSize() which returns 67. However if I do count($block->getCollection()->getData()) the result is 12. The layered navigation is also using the larger number. The issue is on a page with a few bundle products on, so that may be a contributing factor. Then when you go to the second page...
Magento 2: Filter Product Collection using Category Id always return empty set
I have a parent category with subcateogry that contains a lot of product in it, when i tried to filter the product collection using this parent category id, it always return an empty collection: then i tried to print the collection sql query using $collections->getSelect()->__toString(), and it prints: it uses the catalog_category_product tab...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.