Как выбрать связанный продукт случайная коллекция Magento 2.1.2 [дубликат]


Как получить случайную коллекцию сопутствующих товаров, используя приведенный ниже код,

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;
}
Author: Teja Bhagavan Kollepara, 2016-11-11

2 answers

Вы можете попробовать с этой строкой,

$this->_itemCollection->getSelect()->orderRand(); 
 1
Author: Rakesh Jesadiya, 2016-11-11 11:19:40

Попробуйте это

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

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

$products = $products->getItems();
 1
Author: , 2016-11-11 11:35:55