0

I am making a custom module and I want to get 5 random product skus. So far I tried:

$objectManager = ObjectManager::getInstance();

/**
 * @var ProductRepository
 */
$productRepository = $objectManager->create(ProductRepository::class);

/**
 * @var SearchCriteriaBuilder
 */
$searchCriteria = $objectManager->create(SearchCriteriaBuilder::class);
$searchCriteria->addSortOrder(new SortOrder());
$skus = $productRepository->getList($searchCriteria)->getItems();

But how I can define that I want the skus to be retrieved in random order?

2 Answers 2

0

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);
0

To retrieve a list of product SKUs in random order using Magento 2's ProductRepository and SearchCriteriaBuilder, you can modify your code to include a random sort order. Here's how you can achieve this:

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\ObjectManager\ConfigLoaderInterface;

class YourClass
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var ConfigLoaderInterface
     */
    protected $configLoader;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        ConfigLoaderInterface $configLoader
    ) {
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->configLoader = $configLoader;
    }

    public function getRandomProductSkus()
    {
        // Create a random sort order
        $sortOrder = new SortOrder();
        $sortOrder->setField('rand'); // 'rand' is not a native Magento field; you'll need to create a custom attribute for random sorting.

        // Set the random sort order in the search criteria
        $this->searchCriteriaBuilder->addSortOrder($sortOrder);

        // Build the search criteria
        $searchCriteria = $this->searchCriteriaBuilder->create();

        // Get the list of products
        $productList = $this->productRepository->getList($searchCriteria);

        // Extract SKUs from the products
        $skus = [];
        foreach ($productList->getItems() as $product) {
            $skus[] = $product->getSku();
        }

        return $skus;
    }
}
2
  • 'rand' is not a native Magento field; you'll need to create a custom attribute for random sorting <<< How I can do that? Commented Sep 12, 2023 at 11:39
  • Create a 'rand' attribute by default magento 2 admin, i.e., catalog -> attributes, or create a custom attribute using code for getting random SKU's. Commented Sep 13, 2023 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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