I want to get CategoryIds and CategoryName to salected Produts; but in my code; I can give CategoryIds to All Products

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    $productCollection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

    $collection = $productCollection->create()

    foreach ($collection as $product) {
          echo 'Name = '.$product->getName().'<br>';
        foreach ($product->getCategoryIds() as $category_id) {

            echo 'Category =' . $category_id. '<br>';

  • Do you want to filter collection by category ids or category names? Apr 21 '17 at 10:54
  • first of all ; I want to to filter collection by category ids
    – Jack
    Apr 21 '17 at 10:58

Try this. On your collection

$collection->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')->addAttributeToSelect('*')->addAttributeToFilter('category_id', array(
        array('finset' => array('7', '8', )),

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.