3

Tring to create Plugin for elasticSearch resolve function

<?php

namespace Devall\ElasticsearchPlugin\Plugin\Checkout\Controller;

use Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolver;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\Data\Collection;
use Magento\Framework\Api\Search\SearchCriteria;

class EmptyProducts
{

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

    /**
     * @var Collection
     */
    private $collection;

    /**
     * @var string
     */
    private $searchRequestName;

    /**
     * @var int
     */
    private $size;

    /**
     * @var array
     */
    private $orders;

    /**
     * @var int
     */
    private $currentPage;

    /**
     * SearchCriteriaResolver constructor.
     * @param SearchCriteriaBuilder $builder
     * @param Collection $collection
     * @param string $searchRequestName
     * @param int $currentPage
     * @param int $size
     * @param array $orders
     */

    public function __construct(
        SearchCriteriaBuilder $builder,
        Collection $collection,
        string $searchRequestName,
        int $currentPage,
        int $size,
        ?array $orders
    ) {
        $this->builder = $builder;
        $this->collection = $collection;
        $this->searchRequestName = $searchRequestName;
        $this->currentPage = $currentPage;
        $this->size = $size;
        $this->orders = $orders;
    }

    public function afterResolve(SearchCriteriaResolver $searchCriteriaResolver){
        if ($this->size !== 0) {
            $this->builder->setPageSize($this->size);
        }
        $searchCriteria = $this->builder->create();
        $searchCriteria->setRequestName($this->searchRequestName);
        $searchCriteria->setSortOrders($this->orders);
        $searchCriteria->setCurrentPage($this->currentPage - 1);

    return $searchCriteriaResolver;
    }
}

It shows an error like

Exception #0 (BadMethodCallException): Missing required argument $searchRequestName of Devall\ElasticsearchPlugin\Plugin\Checkout\Controller\EmptyProducts.

But why I have no idea. Help.

1

Your arguments in your plugin are wrong, if you take a look at the devdocs for plugins https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html#after-methods.

You can see that for after plugins, first parameter is the class your plugin fires from. The seconds is the result of the original code and all after is the parameters to the original method.

So your plugin should look like so


<?php

namespace Devall\ElasticsearchPlugin\Plugin\Checkout\Controller;

class EmptyProducts
{

    public function afterResolve($subject, $result)
    {
        // Result will be the searchCriteriaResolve return from the original code
        $result->setCurrentPage(0);
        return $result;
    }

}

| improve this answer | |
  • Yes maybe it was my bad and I didn't read everything properly. Thank You! GLORY SAM! – Giga Todadze Feb 26 at 7:47

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.