0

I am currently working on a project where it loads a collection of products with a CategoryFilter.

$category = $this->categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

    return $collection;

From administration panel (Catalog -> Categories), you can choose "manual sorting" or "automatic sorting" from "Display Settings".

But this option seems to not work.

Can someone explain me, what I am suppose to load the collection with admin filters ?

Thanks you.

0

The setting provides at the administration panel will work at frontend when you go to a Category page and magento already build the code according.

If you will want to apply the administration section sorting to your custom collection then you have to write the code.

If you sort field is product attribute then you can use addAttributeToSort('{Attribute_Code}', '{SortOrder}');. This function need to add with your collection.

Suppose, you want to sort by name attribute Ascending order then use add below code before return $collection

 $collection->addAttributeToSort('name', 'asc');
 return $collection;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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