0

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()
        ->addAttributeToSelect('*')
        ->load();

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

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

        }
    }
2
  • Do you want to filter collection by category ids or category names? Commented Apr 21, 2017 at 10:54
  • first of all ; I want to to filter collection by category ids
    – Jack
    Commented Apr 21, 2017 at 10:58

1 Answer 1

0

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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.