Skip to main content
45 votes

Magento 2: Sort order and limit product collection

Just add this code addAttributeToSort('entity_id', 'desc') to your collection. As product follows EAV structure, you can use addAttributeToSort('attribute_code', 'sort_order') For limiting a ...
Amit Bera's user avatar
  • 77.6k
41 votes
Accepted

Magento 2 How can we get product url with product collection?

$productCollection = $productObj; Gives from product collection url using loop the collection, foreach($productCollection as $_product){ echo $_product->getProductUrl(); }
Rakesh Jesadiya's user avatar
19 votes

Faster way to load media images in a product collection

The accepted answer works, but involves loading all the product attributes into memory when perhaps they are unnecessary. To take a slightly more surgical approach (tested in 1.9.2.2 CE) do $...
Aaron Bonner's user avatar
19 votes
Accepted

Magento 2: join custom table with product collection while sql query is running fine

I finallly found solution. Actually collection making alias for join table. You don't have to set alias $joinConditions = 'e.entity_id = store_price.product_id'; $collection->addAttributeToSelect('...
Abhishek's user avatar
  • 939
18 votes
Accepted

Magento2 product collection -> addAttributeToFilter with LIKE operator not working

Use 'like' instead of 'LIKE' $productcollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->...
Sohel Rana's user avatar
  • 35.9k
17 votes

How to load product collection in Magento 2

The standard collections are actually very similar. There are other similar structures, but for products you can load them in and load them just like Magento 1. Just like in Magento 1, if you are ...
Robert Egginton's user avatar
15 votes
Accepted

Magento 2: Filter Product Collection by Multiple Categories (Magento 2.1)

You are probably used to the "every method returns $this" paradigm from Magento 1. This is not the case anymore (at least not always). Specifically, addCategoriesFilter() does not return anything and ...
Fabian Schmengler's user avatar
13 votes
Accepted

How to get Best Selling Product Collection in Magento 2 using Object Manager

Try this $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory'); $...
Dinesh Yadav's user avatar
  • 6,465
12 votes
Accepted

Magento : Filter Collection by Attribute that Doesn't Exist or Null or another condition

After some tests, the code I wrote in the third edit of the question seems to work fine, so I think it is the correct answer: $_products = Mage::getModel('catalog/product') ->getCollection() -&...
Alex's user avatar
  • 811
12 votes
Accepted

How to get Product name in phtml file in magento2

You need to load product to get product name With Object Manager $productId = "10"; //Product Id $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->...
Prince Patel's user avatar
  • 22.8k
11 votes
Accepted

Force products collection to use EAV instead of flat table

The object which is responsible to determine if flat index is available (class Magento\Catalog\Model\Indexer\Product\Flat\State) is an immutable shared instance. But it is possible to use our own ...
Fabian Schmengler's user avatar
10 votes

Magento2 - product collection filter out of stock products

To bypass removing out of stock products in frontend (which could happens if you switched off showing out of stock items at stock options config page) we just need to set up a flag before we load the ...
Mammouth's user avatar
  • 731
10 votes

Magento 2: Product collection with filters

1) We can print the sql with getSelect() echo $productcollection->getSelect(); 2) Add attribute filter to collection vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection::...
Khoa TruongDinh's user avatar
10 votes
Accepted

Magento 2: Custom sort order for product collection(skus, ids etc)

You can do in magento 2 as below way, use \Zend_Db_Expr $collection->getSelect()->order(new \Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $skus).')'));
Rakesh Jesadiya's user avatar
10 votes

Magento 2 : Is there a way to query products using Custom Attributes in Rest API Call?

Resolved the issue by directly using the attribute_code within the search criteria. https://{domain}/rest/all/V1/products/?searchCriteria[filterGroups][0][filters][0][field]=isbn_13&...
Books Lelo's user avatar
10 votes
Accepted

Magento 2 - Call list phtml by passing collection variable

First you need to create block Vendor\Module\Block\ProductList.php by overriding this function _getProductCollection() by extending your class from this Magento\Catalog\Block\Product\ListProduct. ...
Muhammad Hasham's user avatar
9 votes

Magento 2 Get product collection in a custom template block

Instead of using the Object manager u can try: public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Product\...
Sushivam's user avatar
  • 2,639
9 votes

How to load product collection in Magento 2

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $...
megi's user avatar
  • 125
9 votes
Accepted

Using Factory get product collection with product name in Magento 2

There are attributes that may not work properly with getData() such as the product price because getData() contains only main table data. Return full collection object(without getData()) in ...
Prince Patel's user avatar
  • 22.8k
8 votes
Accepted

Magento 2 - Get Collection using custom product Ids and also sort them as the same order as product Ids passed

You just need to add order along with your addAttributeToFilter $collection->addAttributeToFilter('entity_id', array('in' => $productIdsarray )); $collection->getSelect()->order(new \...
Muhammad Hasham's user avatar
7 votes

How to get the product collection based on catalog & cart price rules?

For catalog rule, if you look at vendor/magento/module-catalog-rule/Model/Rule You will find method getMatchingProductIds() which return an array of all matching products for a particular rule. So ...
Amit Singh's user avatar
  • 1,775
7 votes

Magento2 - product collection filter out of stock products

Use below code to your class to filter out of stock product /** * @var \Magento\CatalogInventory\Helper\Stock */ protected $_stockFilter; on class constructor public function __construct( .....
Manish's user avatar
  • 3,116
7 votes
Accepted

Exclude Category when showing product collection

You can left join the catalog_category_product table to match products in your "exclude category", then filter your collection to only return products with no matches in the joined table: This should ...
Fabian Schmengler's user avatar
7 votes

Magento 2 Filter product collection by only stock in products

Magento 1 This snippet provides a collection of in-stock products, or products which do not have stock management enabled on them. $productCollection = Mage::getResourceModel('catalog/...
Shahab Hameed's user avatar
7 votes

Magento 2: How to get product custom attribute in product grid

1/ Magento Admin > Stores > Attributes / Product > Click on your custom attribute > Advanced Attribute Properties > Set "Add to Column Options" to "Yes" and click on "Save attribute" button 2/ ...
Jonathan Ribas's user avatar
7 votes

magento 2 best seller ,most viewed and Wishlist product collection

Here's an example to show how to get these collections: <?php namespace Vendor\Module\Block; use Magento\Framework\View\Element\Template; class Statistics extends Template { protected $...
Aaron Allen's user avatar
  • 9,009
7 votes

How to load product collection in Magento 2

Here is the best way to get product collection in magento public function __construct( \Magento\Catalog\Model\Product $product ) { $this->product = $product; } public function ...
Vaibhav Ahalpara's user avatar
7 votes
Accepted

Get all products of a specific category only - without products of its child categories

Try this: $category = Mage::getModel('catalog/category')->load(5); $isAnchorFlag = $category->getIsAnchor(); $category->setIsAnchor(false); $getCollections = $category->...
Marius's user avatar
  • 198k
7 votes
Accepted

How to get product collection in controller in Magento2

Overview of getting product collection in Magento 2 Step 1: Declare in Vendor_ModuleName Block Step 2: Display product collection in phtml file .. Step 1: Declare in Vendor_ModuleName Block You ...
Baharuni Asif's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible