Magento 2 how to implement catalog search programmatically.

I need categories and product details.

Please provide reference links if any.

Please find the screenshot : enter image description here


EX: http://testsite.com/filename.php?q={yoursearchterm}

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->get(\Magento\Catalog\Model\Layer\Resolver::class);
$layerResolvers = $layerResolver->create('search');
$layerResolvers1 = $layerResolver->get();
$productCollection = $layerResolvers1->getProductCollection();

echo "Search layered navigation :<br>";
echo "products count == ".count($productCollection)."<br>";
foreach ($productCollection as $product) {
    echo "Name : ".$product->getName()." Price : ".$product->getPrice()."<br>";
| improve this answer | |
  • thanks for u r reply but how to get categories here? – Nagaraju K Aug 4 '17 at 7:38
  • Just add the categories on Magento Admin – DevAnd Aug 7 '17 at 18:44

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.