1

This question already has an answer here:

How to get a random collection of related products, using the below code,

protected function _prepareData() 
{
    $product = $this->_coreRegistry->registry('product');
   /* @var $product \Magento\Catalog\Model\Product */

   $this->_itemCollection = $product->getAlsoboughtProductCollection()->addAttributeToSelect(
        'required_options'
    )->setPositionOrder()->setPageSize(3)->addStoreFilter();
    $this->_itemCollection->getSelect()->order('rand()');
    if ($this->moduleManager->isEnabled('Magento_Checkout')) {
        $this->_addProductAttributesAndPrices($this->_itemCollection);
    }
    $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());

    $this->_itemCollection->load();

    foreach ($this->_itemCollection as $product) {
        $product->setDoNotUseCategoryId(true);
    }
    return $this;
}

marked as duplicate by Raphael at Digital Pianism, Qaisar Satti, Murtuza Zabuawala, Rajeev K Tomy, Khoa TruongDinh Nov 14 '16 at 13:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You can try with this line,

$this->_itemCollection->getSelect()->orderRand(); 
  • Sorry, No change still getting the same result – Krishnakumar C Nov 11 '16 at 11:25
  • Have you clear cache? remove var folder and check. – Rakesh Jesadiya Nov 11 '16 at 11:28
1

try this

$products = $this
        ->getCategory($id)
        ->getProductCollection()
        ->addAttributeToSelect('*')
        ->setPageSize(3);

$products->getSelect()->orderRand();

$products = $products->getItems();

Not the answer you're looking for? Browse other questions tagged or ask your own question.