0

I want to display out of stock products at the end of the V1/products response.

So far I tried to create a plugin on Magento\Catalog\Api\ProductRepositoryInterface to override the getList method but it doesn't work.

I don't know if this is a good method or I have to use an Observer or a plugin on another Class

I tried this in a di.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="products-list" type="Custom\Api\Plugin\ProductList"/>
    </type>
</config>

And in ProductList.php :

<?php

namespace Custom\Api\Plugin;

class ProductGet
{

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
    ) {
        $this->collectionFactory = $collectionFactory;
    }

    public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
    {
        // this method isn't called :(
    }
}

Update #1 I added a beforeGetList function :

public function beforeGetList(
        ProductRepositoryInterface $subject,
        SearchCriteriaInterface $searchCriteria
    ) {
        $sortOrders = $searchCriteria->getSortOrders() ?? array();
        $sortOrders[] = $this->sortOrderBuilder->setField('stock_status')->setDirection(SortOrder::SORT_DESC)->create();
        $searchCriteria->setSortOrders($sortOrders);
        return [$searchCriteria];
    }
4
  • Hey @darkmatterantearth, welcome! I'd like to recommend you to share more details about what you did so far. SO, generally speaking, is not here to do the work for you but try to help with specific issues based on your own work (code and/or research). You may want to refer to stackoverflow.blog/2010/10/04/asking-better-questions aswell to find some inforation how you may improve your question. Commented Feb 25, 2022 at 0:33
  • Thanks for your response. I added some details. I'm not looking for a ready-made solution, but for ways to find the best way to do it. Commented Feb 25, 2022 at 8:39
  • Per your code example I guess there is some misinterpretation about what plugins are: "[Plugins] allow[s] you to substitute or extend the behavior of original, public methods for any class or interface" (see also devdocs.magento.com/guides/v2.4/extension-dev-guide/…). That being said, instead of 'redefining' getList() you would want to have a method such as beforeGetList, afterGetList or aroundGetList. Also, class ProductGet should probably be class ProductList; In core Magento you should be able to find a few examples on existing plugins for this class already. Commented Feb 25, 2022 at 15:04
  • Thanks. I added a beforeGetList function (see update #1) But stock_status isn't in the fields of the collection. How can I add a field with $collection->joinField ? Maybe with aroundGetList ? Commented Mar 3, 2022 at 19:33

0

Your Answer

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

Browse other questions tagged or ask your own question.