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.3k
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
21 votes
Accepted

Get product collection with product ids

Given an instantiated but not loaded collection $collection and an array of product ids $productIds, you can use addIdFilter() just as in Magento 1: $collection->addIdFilter($productIds); To ...
Fabian Schmengler'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.7k
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

How to reset a specific order by column?

The available sorting methods are addOrder(), setOrder(), unshiftOrder(), addAttributeToSort() and getSelect()->order() Only setOrder($field, $dir) is available regardless of collection type and ...
Denis Spalenza'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,437
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.7k
11 votes

Get product collection with product ids

Use SearchCriteria and Product Repositories: $productIds = [.....]; $searchCriteria = $this->searchCriteriaBuilder ->addFilter('entity_id', $productIds, 'in') -&...
Phoenix128_RiccardoT's user avatar
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,629
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.7k
8 votes
Accepted

Magento2 - Custom product attribute in product collection

Based on the comments... when using an attribute in a product collection, if the flat catalog is enabled, the attribute must be marked as Used in product listing in order to be picked up by the ...
Marius's user avatar
  • 198k
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,745
7 votes
Accepted

Magento2: Filter ProductCollection by Product Type (type_id)

Please, remove load() from the line $this->_productCollection->load();, because you can not filter the collections that has already been loaded.
Siarhey Uchukhlebau's user avatar
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,106
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

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