1

I have a build service contact on an entity of custom module

And it has 3 fields like pk,order_id.order_item.

And When try to apply setFilterGroups on SearchCriteria getting Error.

Uncaught TypeError: Argument 1 passed to Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor: :addFilterGroupToCollection() must be an instance of Magento\Framework\Api\Search\FilterGroup, instance of Magento\Framework\Api\Filter given, called in /var/www/html/vendor/magento/framework/Api/SearchCriteria/CollectionProcessor/FilterProcessor.php on line 48 and defined in /var/www/html/vendor/magento/framework/Api/SearchCriteria/CollectionProcessor/FilterProcessor.php: 59

Here my Repository class:

<?php

declare(strict_types=1);

namespace AmitBera\WarrantyCode\Model;

use AmitBera\WarrantyCode\Api\Data\TransactionInfoInterface;
use AmitBera\WarrantyCode\Api\Data\TransactionInfoSearchResultsInterface;
use AmitBera\WarrantyCode\Api\Data\TransactionInfoSearchResultsInterfaceFactory;
use AmitBera\WarrantyCode\Api\TransactionInfoRepositoryInterface;
use AmitBera\WarrantyCode\Model\ResourceModel\TransactionInfo as TransactionInfoResourceModel;
use AmitBera\WarrantyCode\Model\ResourceModel\TransactionInfo\CollectionFactory;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;

class TransactionInfoRepository implements TransactionInfoRepositoryInterface
{

    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    /**
     * @var SortOrderBuilder
     */
    private $sortOrderBuilder;

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var CollectionProcessorInterface
     */
    private $collectionProcessor;

    /**
     * @var JoinProcessorInterface
     */
    private $extensionAttributesJoinProcessor;

    /**
     * @var TransactionInfoSearchResultsInterfaceFactory
     */
    private $transactionInfoSearchResultsInterfaceFactory;

    /**
     * @var TransactionInfoResourceModel
     */
    private $transactionInfoResourceModel;

    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @var TransactionInfoFactory
     */
    private $transactionInfoFactory;

    public function __construct(
        TransactionInfoFactory $transactionInfoFactory,
        TransactionInfoResourceModel $transactionInfoResourceModel,
        CollectionFactory $collectionFactory,
        JoinProcessorInterface $extensionAttributesJoinProcessor,
        CollectionProcessorInterface $collectionProcessor,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterBuilder $filterBuilder,
        SortOrderBuilder $sortOrderBuilder,
        TransactionInfoSearchResultsInterfaceFactory $transactionInfoSearchResultsInterfaceFactory
    ) {
        $this->transactionInfoFactory = $transactionInfoFactory;
        $this->collectionFactory = $collectionFactory;
        $this->transactionInfoResourceModel = $transactionInfoResourceModel;
        $this->transactionInfoSearchResultsInterfaceFactory = $transactionInfoSearchResultsInterfaceFactory;
        $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
        $this->collectionProcessor = $collectionProcessor;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->sortOrderBuilder = $sortOrderBuilder;
        $this->filterBuilder = $filterBuilder;
    }

    /**
     * @inheritdoc
     */
    public function delete(TransactionInfoInterface $transactionInfo)
    {
        try {
            $this->transactionInfoResourceModel->delete($transactionInfo);
        } catch (\Exception $exception) {
            throw new CouldNotDeleteException(__($exception->getMessage()));
        }
        return true;
    }

    /**
     * @inheritdoc
     */
    public function deleteById($transactionId)
    {
        return $this->delete($this->getById($transactionId));
    }

    /**
     * @inheritdoc
     */
    public function getById($transactionId): TransactionInfoInterface
    {
        $transactionInfo = $this->transactionInfoFactory->create();
        $this->transactionInfoResourceModel->load($transactionInfo, $transactionId);

        if (!$transactionInfo->getId()) {
            throw new NoSuchEntityException(__('The transaction info with the "%1" ID doesn\'t exist.', $activationId));
        }
        return $transactionInfo;
    }

    /**
     * @inheritdoc
     */
    public function getList(SearchCriteriaInterface $searchCriteria = null): TransactioninfoSearchResultsInterface
    {
        $collection = $this->collectionFactory->create();

        if (null === $searchCriteria) {
            $searchCriteria = $this->searchCriteriaBuilder->create();
        }
        $this->extensionAttributesJoinProcessor->process($collection);
        $this->collectionProcessor->process($searchCriteria, $collection);


        $searchResult = $this->transactionInfoSearchResultsInterfaceFactory->create();
        $searchResult->setItems($collection->getItems());
        $searchResult->setTotalCount($collection->getSize());
        $searchResult->setSearchCriteria($searchCriteria);
        return $searchResult;
    }

    /**
     * @inheritdoc
     */
    public function save(\AmitBera\WarrantyCode\Api\Data\TransactionInfoInterface $transactionInfo)
    {
        try {
            $this->transactionInfoResourceModel->save($transactionInfo);
        } catch (\Exception $exception) {
            throw new CouldNotSaveException(
                __('Could not save the page: %1', $exception->getMessage()),
                $exception
            );
        }

        return $transactionInfo;
    }

    /**
     * @inheritDoc
     */

    public function saveActivationCodeToOrderGrid($orderId)
    {
        $this->transactionInfoResourceModel->assignActivationCodeToOrderGrid($orderId);
        return true;
    }
}

2. The Class where I have applied that and condition using filter Group

<?php

declare(strict_types=1);

namespace AmitBera\WarrantyCode\Plugin\Magento\Sales\Api;

use AmitBera\WarrantyCode\Api\TransactionInfoRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\Search\FilterGroupBuilder;

class OrderRepositoryInterface
{

    /**
     * @var FilterGroupBuilder
     */
    private $filterGroupBuilder;

    /**
     * @var FilterGroup
     */
    private $filterGroup;
    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    /**
     * @var \AmitBera\WarrantyCode\Api\TransactionInfoRepositoryInterface
     */
    private $transactionInfoRepository;

    public function __construct(
      TransactionInfoRepositoryInterface $transactionInfoRepository,
      SearchCriteriaBuilder $searchCriteriaBuilder,
      FilterBuilder $filterBuilder,
      FilterGroupBuilder $filterGroupBuilder,      
      FilterGroup $filterGroup
    ){

        $this->transactionInfoRepository = $transactionInfoRepository;
        $this->filterBuilder = $filterBuilder;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterGroup = $filterGroup;
        $this->filterGroupBuilder = $filterGroupBuilder;
    }   

    /**
     * Get gift message
     *
     * @param \Magento\Sales\Api\OrderRepositoryInterface $subject
     * @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
     * @return \Magento\Sales\Api\Data\OrderInterface
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGet(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        \Magento\Sales\Api\Data\OrderInterface $resultOrder
    ) {
        $resultOrder = $this->getOrderItemActivationCodes($resultOrder);
        return $resultOrder;
    }

    private function getOrderItemActivationCodes(\Magento\Sales\Api\Data\OrderInterface $order)
    {
         $orderItems = $order->getItems();

        if (null !== $orderItems) {

            /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
            foreach ($orderItems as $orderItem) {
                $extensionAttributes = $orderItem->getExtensionAttributes();
                if ($extensionAttributes && $extensionAttributes->getActivationCodes()) {
                    continue;
                }

                $transactionInfos= $this->getActivationCodeForOrderItem((int)$order->getEntityId(),$orderItem);

                if($transactionInfos->getTotalCount() < 0){
                    continue;
                }

                /** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
                $orderItemExtension = $extensionAttributes
                    ? $extensionAttributes
                    : $this->orderItemExtensionFactory->create();
                $orderItemExtension->setActivationCodes($transactionInfos->getItems());
                $orderItem->setExtensionAttributes($orderItemExtension);                

            }   
        }    
        return $order;
    }

    /**
     * Get an order item activation code info
     * @param int $orderId
     * @param \Magento\Sales\Api\Data\OrderItemInterface $orderItem
     * @return \AmitBera\WarrantyCode\Api\Data\TransactionInfoSearchResultsInterface
     */
    private function getActivationCodeForOrderItem($orderId,$orderItem)
    {

        $orderFilter = $this->filterBuilder
                ->setField('order_id')->setValue($orderId)->setConditionType('eq')
                ->create();
        $orderFilterGroup = $this->filterGroupBuilder->addFilter($orderFilter)->create();

        $orderItemFilter =$this->filterBuilder->setField('item_id')->setValue((int)$orderItem->getItemId())
                ->setConditionType('eq')->create();

        $orderItemFilterGroup = $this->filterGroupBuilder->setFilters([$orderItemFilter])->create();

        $this->searchCriteriaBuilder->setFilterGroups([$orderFilterGroup,$orderItemFilter]);

        $searchCriteria = $this->searchCriteriaBuilder->setPageSize(5)->setCurrentPage(1)->create();

        $transactionInfos= $this->transactionInfoRepository->getList($searchCriteria);
        return $transactionInfos;

    }
}

I have tried to apply a simple condition like:

Order=X and Order_item_id=Y

But it does not work.

Anybody has an idea. where I doing the mistake

Error:

Argument 1 passed to Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor: :addFilterGroupToCollection() must be an instance of Magento\Framework\Api\Search\FilterGroup, instance of Magento\Framework\Api\Filter

5

Try the following code:

private function getActivationCodeForOrderItem($orderId, $orderItem)
{

    $orderFilter = $this->filterBuilder
        ->setField('order_id')->setValue($orderId)->setConditionType('eq')
        ->create();
    $orderFilterGroup = $this->filterGroupBuilder->setFilters([$orderFilter])->create();

    $orderItemFilter =$this->filterBuilder->setField('item_id')->setValue((int)$orderItem->getItemId())
        ->setConditionType('eq')->create();
    $orderItemFilterGroup = $this->filterGroupBuilder->setFilters([$orderItemFilter])->create();

    $this->searchCriteriaBuilder->setFilterGroups([$orderFilterGroup, $orderItemFilterGroup]);

    $searchCriteria = $this->searchCriteriaBuilder->setPageSize(5)->setCurrentPage(1)->create();

    $transactionInfos= $this->transactionInfoRepository->getList($searchCriteria);
    return $transactionInfos;
}
| improve this answer | |
  • Thanks @Sohel . But setFilters([$orderFilter, $orderItemFilter]) behave like OR – Amit Bera Dec 19 '19 at 16:44
  • yes, you can extract. Check updated answer – Sohel Rana Dec 19 '19 at 16:45
  • Sohel, i have tried extract same ` $this->searchCriteriaBuilder->setFilterGroups([$orderFilterGroup, $orderItemFilterGroup]);` ,See my answer, but getting the error Argument 1 passed to Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor: :addFilterGroupToCollection() must be an instance of Magento\Framework\Api\Search\FilterGroup, instance of Magento\Framework\Api\Filter – Amit Bera Dec 19 '19 at 16:50
  • That is issue for my case. and using 2.3.2 CE – Amit Bera Dec 19 '19 at 16:51
  • Wrong, you should try my answer. Check the typo here $this->searchCriteriaBuilder->setFilterGroups([$orderFilterGroup,$orderItemFilter]); – Sohel Rana Dec 19 '19 at 17:04

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.