문제

I am filtering product collection like below.

    $productSkuString = 'O, C, D, A, F, S';
        $productSkus = explode(', ', $productSkuString); 
        $collection = $this->collectionFactory->create()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter(
                'sku', array('in' => $productSkus)
        )           
        ->setStoreId(0)
        ->load();
        return $collection;

The above code filtering the product collection with mentioned skus. but not in the same order of the sku.

I need to load the product with same order where the sku is mentioned.

Currently its loading the product in random order.

Can anyone look into it and update me please

도움이 되었습니까?

해결책

Use setOrder as below to sort product collection

 $productSkuString = 'O, C, D, A, F, S';
 $collection = $this->collectionFactory->create()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter(
                'sku', array('in' => $productSkus)
        )           
        ->setStoreId(0)
        ->setOrder(new Zend_Db_Expr("FIND_IN_SET('sku', ".$productSkuString.") ASC"))
        ->load();

Reference:

https://stackoverflow.com/a/3265557/10540508

https://magento.stackexchange.com/a/137522/75828

다른 팁

Use below code

$productSkus = array("O","C","D","A");
$collection = $this->collectionFactory->create()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter(
                'sku', array('in' => $productSkus)
        )           
        ->setStoreId(0)
        ->setOrder('sku','ASC')
        ->load();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top