0

I got the task to port some custom build stuff from magento 1 to magento 2. One function was the extension of the order api, now the OrderRepository.

In theory I got it running, but my problem is, that the filterGroups of the searchCriteria is empty. We used the filterGroups as a container for some custom parameter to execute a SQL filter on the order collection.

e.g. as a parameter we use invoiced_at and adding an SQL to the collection where we collect all orders which invoice is created_at = invoiced_at

Is there any way to get and edit the filterGroups of the searchCriteria?

di.xml

<?xml version="1.0"?>
<config
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <type name="Magento\Sales\Model\OrderRepository">
        <plugin name="company_orderrepository_override"
            type="Company\SystemOverride\Model\Plugin\OrderRepositoryOverride" sortOrder="1" />
    </type>

</config>

OrderRepositoryOverride.php

<?php
namespace Company\SystemOverride\Model\Plugin;

class OrderRepositoryOverride {

    protected $_request;
    protected $_logger;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_request = $request;
        $this->_logger = $logger;
    }

    public function aroundGetList(
            $subject,
            $proceed,
            \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) {
                $this->_logger->debug("_request: " . json_encode($this->_request));
                $this->_logger->debug("subject: " . json_encode(get_class_methods($subject)));
                $this->_logger->debug("proceed: " . json_encode(get_class_methods($proceed)));
                $this->_logger->debug("get_class_methods->searchCriteria: " . json_encode(get_class_methods($searchCriteria)));
                $this->_logger->debug("searchCriteria: " . json_encode($searchCriteria));
                $this->_logger->debug("getFilterGroups: " . json_encode($searchCriteria->getFilterGroups()));
                $this->_logger->debug("getSortOrders: " . json_encode($searchCriteria->getSortOrders()));
                $this->_logger->debug("getPageSize: " . json_encode($searchCriteria->getPageSize()));
                $this->_logger->debug("getCurrentPage: " . json_encode($searchCriteria->getCurrentPage()));
                $searchResult = $proceed($searchCriteria);
                return $searchResult;
    }
}

debug.log

main.DEBUG: _request: {} [] []
main.DEBUG: subject: ["__construct","get","getList","save","delete","deleteById","___init","___callParent","__sleep","__wakeup"] [] []
main.DEBUG: proceed: ["bind","bindTo","call","fromCallable"] [] []
main.DEBUG: get_class_methods->searchCriteria: ["getFilterGroups","getSortOrders","getPageSize","getCurrentPage","setFilterGroups","setSortOrders","setPageSize","setCurrentPage","__construct","setData","__toArray"] [] []
main.DEBUG: searchCriteria: {} [] []
main.DEBUG: getFilterGroups: [{}] [] []
main.DEBUG: getSortOrders: null [] []
main.DEBUG: getPageSize: 25 [] []
main.DEBUG: getCurrentPage: 0 [] []

rest response

{
  "items": [],
  "search_criteria": {
    "filter_groups": [
      {
        "filters": [
          {
            "field": "created_at",
            "value": "2020-01-20",
            "condition_type": "eq"
          }
        ]
      }
    ],
    "page_size": 25,
    "current_page": 0
  },
  "total_count": 0
}
0

Found the solution :)

print_r helped a lot

main.DEBUG: Magento\Framework\Api\SearchCriteria Object (
    [_data:protected] => Array (
        [filter_groups] => Array (
            [0] => Magento\Framework\Api\Search\FilterGroup Object (
                [_data:protected] => Array (
                    [filters] => Array (
                        [0] => Magento\Framework\Api\Filter Object (
                            [_data:protected] => Array (
                                [field] => created_at
                                [value] => 2020-01-20
                            )
                        )
                    )
                )
            )
        )
        [page_size] => 25
        [current_page] => 0
    )
)

$searchCriteria->getFilterGroups() was an array containing an another array from $group->getFilters()

$this->_logger->debug(print_r($searchCriteria, true));
foreach ($searchCriteria->getFilterGroups() as $group) {
    $this->_logger->debug(print_r($group, true));
    foreach ($group->getFilters() as $filter) {
      $this->_logger->debug(print_r($filter, true));
    }
}

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.