I'm trying to filter my product search by returning only products within the current category I'm in, I can't find where the product collection is created within the search module

Can anyone help point me in the right direction?

  • Please confirm whether you would like to filter products by category id ? – Nagaraju K Jul 28 '17 at 9:41
  • @NAGARAJUKASA Yes that's right! – Tom Jul 28 '17 at 9:45
  • just i am giving u sample prototype script with the example let's try and get back with u r suggestions thanks – Nagaraju K Jul 28 '17 at 9:52
  • @NAGARAJUKASA Thank you, when i run this is just redirects me to the homepage, do I put it within my theme or in the whole file root (public_html/test.php) ? – Tom Jul 28 '17 at 10:27
  • you have to keep in magento 2 root folder ex: prntscr.com/g1d98w – Nagaraju K Jul 28 '17 at 10:33

Please run the below code in the default magento2 at root (test.php), Here i am giving u sample example with the filter options and returning the collection array.

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cateid = '9';
$cateinstance = $objectManager->get('Magento\Catalog\Model\CategoryFactory');
$allcategoryproducts = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
echo  "<pre>";
echo count($allcategoryproducts);?>

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.