0

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

0

2 Answers 2

2

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 */
$productCollection->load();
-7

** 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:

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


$_productCollection = Mage::getModel('catalog/product')
 ->getCollection()
 ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status','1')
 ->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);
1
  • 2
    The question is Magento2 related. Your code is for Magento 1.
    – Marius
    Commented Dec 2, 2015 at 7:12

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