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()){
        parent::__construct($context, $data);
    public function getPromoDetail(){
        $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) {
        return $sales_rule[0]    ;


Try this way:

$searchCriteria = $searchCriteriaBuilder->create();

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

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.