We are trying to get out In stock products to show before our out of stock products on our site. There appears to be no options for this in the magento settings so I thought of looking in the code with in /vendor/magento/module-catalog/Model/Layer.php. I found these functions

public function getProductCollection()
    if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
        $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
    } else {
        $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
        $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
    return $collection;

 * Initialize product collection
 * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
 * @return \Magento\Catalog\Model\Layer
public function prepareProductCollection($collection)
    $this->collectionFilter->filter($collection, $this->getCurrentCategory());

    return $this;

I am just unsure if these are the right ones that get all the products, or what I should do with them.

I tried using this which I found from another post with no success only errors:

$this->_productCollection->getSelect()->order('is_salable DESC');

also this from that same post:

$collection->joinField('in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id', 'in_stock>=0', 'left')->setOrder('in_stock','desc');

with again no success.

Your Answer

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

Browse other questions tagged or ask your own question.