For Magento 2.2 I'm using:-


$ct = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); 
        $collections = $ct->getProductCollection()
                     ->addAttributeToFilter('status', 1) 
                     ->addAttributeToSort('sort_value', 'DESC');

    echo "<pre>Test" ; print_r($collections->getData());exit;

& getting 15 records.

but currently, 'sort_value' attribute is empty for all 15 records. & whenever I'm refreshing pages each time collection is shuffling while the same script is giving fixed collection for Magento 1.

I needed fixed collection means whenever I refreshed the page it should remain same.

For a product, I assigned sort_value=10 just for testing & now this product is coming always on top.while rest of the 14 products(having empty sort_value) are shuffling.

Note:-1 the type of sort_value is price.

2- I've used addAttributeToSort('sort_value', 'DESC'); on product listing & it gives expected result.

Please help me to resolve this issue.

  • Did you checked the attribute is filterable ? – Elavarasan Sep 14 '18 at 11:42
  • 1
    Add this query secondary soring addAttributeToSort('entity_id', 'DESC') after addAttributeToSort('sort_value', 'DESC') as secondary shorting – Amit Bera Sep 14 '18 at 11:59
  • @Elavarasan, thanks for the reply! yes this is filterable.i already explained in the post. – wakar Ahamad Sep 14 '18 at 12:26
  • @Amit,thanks for the reply! same thing is working for product listing.actually according to my requirement i can't use addAttributeToSort('entity_id', 'DESC') because i'm only sorting product listing by using only 'sort_value'.so according to product listing i've to show 4 neighbour(based on some condition) products on the product detail page. – wakar Ahamad Sep 14 '18 at 12:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.