How to get product details in magento 2. How to do something like below in Magento2 Mage::getModel('catalog/product')->getCollection()


Try this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
/** Apply filters here */

** Below answer is for Magento-1, ignore for Magento-2 **

if you want to use magento functions/models outside magento, just call app/Mage.php file in your outside file.

1. To get product collection bases on category ID use below code:

 $categoryIds = array(Mage::registry('current_category')->getId());//category id

$_productCollection = Mage::getModel('catalog/product')
 ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
 ->addAttributeToFilter('category_id', array('in' => $categoryIds));
<?php if(!$_productCollection->count()): ?>
    <?php foreach ($_productCollection as $_product): ?>
        <?php $product =  Mage::getModel('catalog/product')->load($_product->getId());?>
            <h2 class="product-name medium"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
    <?php endforeach; ?>
<?php endif; ?>

2. For all products of your current store :

$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id); // default store Id is 1

   $collection ->setPageSize(6);
  • 2
    The question is Magento2 related. Your code is for Magento 1.
    – Marius
    Dec 2 '15 at 7:12

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