Skip to main content
added 1 character in body
Source Link

Well using ProductRepository Isis not an option in your case. How about using the ProductCollectionFactory Instead:instead?


use Magento\Framework\App\ObjectManager;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;


// Rest of code reside here (eg. class definition)

 $objectManager = ObjectManager::getInstance();

/**
 * @var ProductCollectionFactory
 */
$productCollectionFactory = $objectManager->create(ProductCollectionFactory::class);

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
$productCollection = $productCollectionFactory->create();
$productCollection->setPageSize(5);
$productCollection->getSelect()->orderRand('sku');

$skus = $productCollection->load()->getData();
$skus = array_map(function($item){return $item['sku'];},$skus);

Well using ProductRepository Is not an option in your case How about using the ProductCollectionFactory Instead:


use Magento\Framework\App\ObjectManager;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;


// Rest of code reside here (eg. class definition)

 $objectManager = ObjectManager::getInstance();

/**
 * @var ProductCollectionFactory
 */
$productCollectionFactory = $objectManager->create(ProductCollectionFactory::class);

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
$productCollection = $productCollectionFactory->create();
$productCollection->setPageSize(5);
$productCollection->getSelect()->orderRand('sku');

$skus = $productCollection->load()->getData();
$skus = array_map(function($item){return $item['sku'];},$skus);

Well using ProductRepository is not an option in your case. How about using the ProductCollectionFactory instead?


use Magento\Framework\App\ObjectManager;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;


// Rest of code reside here (eg. class definition)

 $objectManager = ObjectManager::getInstance();

/**
 * @var ProductCollectionFactory
 */
$productCollectionFactory = $objectManager->create(ProductCollectionFactory::class);

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
$productCollection = $productCollectionFactory->create();
$productCollection->setPageSize(5);
$productCollection->getSelect()->orderRand('sku');

$skus = $productCollection->load()->getData();
$skus = array_map(function($item){return $item['sku'];},$skus);
Source Link

Well using ProductRepository Is not an option in your case How about using the ProductCollectionFactory Instead:


use Magento\Framework\App\ObjectManager;
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;


// Rest of code reside here (eg. class definition)

 $objectManager = ObjectManager::getInstance();

/**
 * @var ProductCollectionFactory
 */
$productCollectionFactory = $objectManager->create(ProductCollectionFactory::class);

/**
 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
$productCollection = $productCollectionFactory->create();
$productCollection->setPageSize(5);
$productCollection->getSelect()->orderRand('sku');

$skus = $productCollection->load()->getData();
$skus = array_map(function($item){return $item['sku'];},$skus);