4

I want to get only the first item from the SearchCriteriaBuilder result is there any method that gives direct first item? currently using getList()->getItems() then for loop forget single item.... so I just want to know is there any shortcut method which gives the first record.....

class Promo extends \Magento\Framework\View\Element\Template{
    protected $sales_rule;
    protected $search_builder;
    public function __construct(Template\Context $context,\Magento\SalesRule\Api\RuleRepositoryInterface $sales_rule,\Magento\Framework\Api\SearchCriteriaBuilder $search_builder,array $data = array()){
        $this->sales_rule=$sales_rule;
        $this->search_builder=$search_builder;
        parent::__construct($context, $data);
    }
    public function getPromoDetail(){
        $sales_rule=array();
        $searchCriteria = $this->search_builder->addFilter('is_active',1,'eq')->addFilter('is_popup_rule',1,'eq')->create();
        $items = $this->sales_rule->getList($searchCriteria);
        foreach ($items->getItems() as $value) {
            $sales_rule[]=$value;
        }
        return $sales_rule[0]    ;

    }
}
5

Try this way:

$searchCriteriaBuilder->setFilterGroups([$filterGroup]);
$searchCriteria = $searchCriteriaBuilder->create();
$searchCriteria->setPageSize(1)
               ->setCurrentPage(1);

$productList = $this->productRepository->getList($searchCriteria);
$item = current($productList->getItems());
| improve this answer | |
1

I think you can use reset function

$searchResults = $this->sales_rule->getList($searchCriteria)->getItems();
$firstItem = reset($searchResults);
| improve this answer | |
  • 1
    Hmmm.. It's not said that the first element has index 0 – Daniele Rovatti Apr 20 at 16:03

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.