cancel
Showing results for 
Search instead for 
Did you mean: 

how to get the search result data from Magento backed

SOLVED

how to get the search result data from Magento backed

Hi 
i want to get the search result product id and there variety data

use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;

.... 

....

 $str    = $search = $json = '';
        $search = $this->_request->getParam('q');       
        $productIdList  = [];
        $productdata    = $this->collection->addSearchFilter($search);
        foreach ($productdata as $product) {
             $productIdList[] = $product->getId();
        }
1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to get the search result data from Magento backend

 

Hi  done the search 

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Netcore\Cee\Helper\Data $helper,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\View\Page\Title $pageTitle,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $collection,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
       \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       \Magento\Backend\Block\Template\Context $context_mage_back,
       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
      
        array $data = []
    ) {
    
        $this->helper           = $helper;
        $this->_request         = $request;
        $this->_pageTitle       = $pageTitle;
        $this->_urlInterface    = $urlInterface;
        $this->_catalogSession  = $catalogSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession;
        $this->productCollectionFactory = $productCollectionFactory;
        $this->categoryFactory = $categoryFactory;
        $this->collection       = $collection;
        $this->productFactory = $productFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;
        $this->addData(['cache_lifetime' => false]);
        parent::__construct($context, $data);
        // parent::__construct($context_mage_back, $data);
    }//end __construct()


//
// protected variables 
///

 $search = $this->_request->getParam('q');       
        $productIdList  = [];
        $productdata    = $this->collection->addSearchFilter($search);
        

        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect(['id','sku']);
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
        $collection->addAttributeToFilter('name', ['like' => '%'.$search.'%']);
        $collection->setPageSize(5);
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

 foreach ($collection as $_product) {
          
            // echo $_product->getName() . ' - ' . $_product->getProductUrl() . '<br />'; 
        }
       

View solution in original post

4 REPLIES 4

Re: how to get the search result data from Magento backed

Dominos Menu Prices: Did you know that there are 500 Domino’s restaurants in Canada? Here’s the most current Domino’s Price List in Canada and interesting and fun Domino’s pizza-related facts that all pizza lovers should know.

Re: how to get the search result data from Magento backed


@MyAARPMedicare wrote:

Hi 
i want to get the search result product id and there variety data

use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;

.... 

....

 $str    = $search = $json = '';
        $search = $this->_request->getParam('q');       
        $productIdList  = [];
        $productdata    = $this->collection->addSearchFilter($search);
        foreach ($productdata as $product) {
             $productIdList[] = $product->getId();
        }

When you use V1/search, you are searching for products through the catalog. The response comes from the configured search engine (Stores > Settings > Configuration > Catalog > Catalog Search > Search engine), and the results are the same as those returned by using the regular frontend search.

All search queries run against the catalogsearch_fulltext index.

Re: how to get the search result data from Magento backend

 

Hi  done the search 

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Netcore\Cee\Helper\Data $helper,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\View\Page\Title $pageTitle,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Catalog\Model\Session $catalogSession,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $collection,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
       \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       \Magento\Backend\Block\Template\Context $context_mage_back,
       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
      
        array $data = []
    ) {
    
        $this->helper           = $helper;
        $this->_request         = $request;
        $this->_pageTitle       = $pageTitle;
        $this->_urlInterface    = $urlInterface;
        $this->_catalogSession  = $catalogSession;
        $this->_checkoutSession = $checkoutSession;
        $this->_customerSession = $customerSession;
        $this->productCollectionFactory = $productCollectionFactory;
        $this->categoryFactory = $categoryFactory;
        $this->collection       = $collection;
        $this->productFactory = $productFactory;
        $this->productStatus = $productStatus;
        $this->productVisibility = $productVisibility;
        $this->addData(['cache_lifetime' => false]);
        parent::__construct($context, $data);
        // parent::__construct($context_mage_back, $data);
    }//end __construct()


//
// protected variables 
///

 $search = $this->_request->getParam('q');       
        $productIdList  = [];
        $productdata    = $this->collection->addSearchFilter($search);
        

        $collection = $this->productCollectionFactory->create();
        $collection->addAttributeToSelect(['id','sku']);
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
        $collection->addAttributeToFilter('name', ['like' => '%'.$search.'%']);
        $collection->setPageSize(5);
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());

 foreach ($collection as $_product) {
          
            // echo $_product->getName() . ' - ' . $_product->getProductUrl() . '<br />'; 
        }
       

Re: how to get the search result data from Magento backed

@bayerruffgd439  Thanks for the answer but this was stop working  for me on Magento 2.3 and 2.4  versions.