Questions tagged [product-collection]

Questions related to Magento product collections.

Filter by
Sorted by
Tagged with
24
votes
3answers
15k views

Faster way to load media images in a product collection

TL;DR: How do I load the product images/gallery without loading the whole product? I want to load the images on a product. What I do in the .phtml $_popularCollection = $this->...
23
votes
6answers
54k views

How to get first item from product collection

On the category view.phtml if I get the category and then get a product collection, how can I get the first product form the collection assigned to $_product for use in the code? Here is what I have: ...
19
votes
1answer
31k views

Magento 2: Sort order and limit product collection

I need to sort the product collection by ID order DESC and add limit to product collection. Here is my code: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $...
18
votes
7answers
103k views

Get product collection from a category id

I am trying to get a collection of products from a category id. A few things I have tried are in the block : $category = Mage::getModel('catalog/category')->load(123) ->...
18
votes
4answers
10k views

Magento 2 - Render custom product collection via list.phtml

The product grid of a category page (frontend) is rendered via layout in catalog_category_view.xml. Lets say I have a custom product collection (which I got via ProductRepositoryInterface::getList(...
15
votes
2answers
34k views

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

I am able to load product collection by using [$objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')] in my phtml file.But in that collection product url is not there ...
12
votes
4answers
20k views

How to filter products NOT IN categories?

Here is my code: $catIds = array(7,8,9); $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect("*"); ->addAttributeToFilter('category_ids', array('...
12
votes
1answer
1k views

Magento2 Custom Layered Navigation

I want to add a layered navigation block for a custom product collection. Is there a way to do that in Magento2 ?
11
votes
2answers
12k views

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

I'm using Magento 2.1.0 and I'm currently having difficulties in filtering product collection with multiple categories. I've used more than a couple of ways to make it work but it won't. Assuming: $...
10
votes
1answer
17k views

Collection with FIND_IN_SET on multiselect attributes, but some has only one value

I am building a product collection, which will find results from a multiselect attribute. (relevant code that adds finset to collection - adjusted to display real id values) $attribute = Mage::...
10
votes
4answers
908 views

Magento 2 - Call list phtml by passing collection variable

I have collection loaded in variable $collection and I want to pass collection variable in my list.phtml $block = $viewBlock->getLayout() ->createBlock('Vendor\Module\Block\...
10
votes
3answers
3k views

Force products collection to use EAV instead of flat table

In Magento 2, how can I temporarily disable the flat catalog? I have a product collection associated with a frontend store and want it to be loaded via EAV tables. I looked how the collections ...
9
votes
5answers
15k views

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

I have created a new product eav attribute, assume it is created correctly. Now I write something in this attribute in some products (5 for example), and I delete it from some of them (4). So this ...
9
votes
5answers
5k views

How to get product collection of out of stock items - opposite of addInStockFilterToCollection()?

I have a requirement to display a category's products in two lists - one for in stock items, the other for out of stock items. I'm using Mage::getSingleton('cataloginventory/stock')->...
9
votes
4answers
1k views

Load Simple Products Collection (both, in stock and out of stock)

I got a problem loading ALL "child" products of a Configurable Product into a collection, even those that are out of stock. Like loading the products like this: $simpleCollection = $configurable->...
9
votes
2answers
1k views

Product collection problem - incorrect pricing data or not showing out of stock items

I am retrieving a product collection based on the current category. I need this collection to contain both in and out of stock products, and I also need the pricing data to be displayed correctly. My ...
9
votes
2answers
2k views

Filter Product Collection by Child product attribute

I have situation like 2 attribute which is assign to child product 1) person_height_from and 2) person_height_to which has DropDown type attribute which is only assign in child product but not ...
8
votes
3answers
12k views

Get product collection with product ids

Is it possible to get a product collection bassd on array of product ids?
7
votes
3answers
29k views

Get Product Collection Containing In-Stock Products Only

There may be a simple answer to this, but I can't find it. I'm trying to retrieve a product collection, excluding products that are enabled, but out of stock. I've tried the following approach, but ...
7
votes
3answers
10k views

Four Random Products on Homepage

I'm trying to get 4 random products on the homepage using PHP within a TPL file I've created. I'd like to be able to format the products in a foreach loop as I'm using some formatting in the code seen ...
7
votes
1answer
5k views

Set default value to custom attribute for all products

I have created the custom attribute (test) for products as a text field with default value('test') from admin panel And assigned that attribute to default attribute set. Now I can able to see the ...
7
votes
3answers
1k views

how to add default products to related products list

I want to add default products of certain ids(example- 1,5,7,3) to be added to the list of related products always with all the products at the end for the product page and need not store in database, ...
7
votes
2answers
2k views

Filter Product Collection By Another Attribute In Product Collection eg 'attribX', array('gt' => 'attrib-Y')

When using product collections like so: $_productCollection= Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('*') ->...
7
votes
1answer
1k views

Show different number of products in category page

I am trying to show different number of products in category but it is only working on page 1. I want to show 22 products on page1 and rest all the pages 20 products per page. I managed to change by ...
6
votes
10answers
63k views

How to load product collection in Magento 2

How to load product collection in Magento 2 and what is the difference between Magento 1 and Magento 2 load product collection
6
votes
2answers
33k views

How to get product category id on product detail page Magento 2

I am trying to get product categories on product detail page. I tried using $categoryIds = $_product->getCategoryIds(); But it is not working for me. Also i tried $categories = $product-&...
6
votes
4answers
42k views

Magento 2 Get product collection in a custom template block

I have defined a featured product template in a cms static page as below: {{block class="Magento\Catalog\Block\Product\ListProduct" template="Magento_Catalog::product/featured.phtml"}} Now I want to ...
6
votes
5answers
68k views

Magento 2: Product collection with filters

Can anyone tell me how can i get product collection with filters? I'm use magento 2 sample data now. So i'm trying to get in product collection like selected on picture with attribure style=backpack ...
6
votes
3answers
11k views

Remove Item By key Using with Own attribute

I am using the Function $collection->removeItemByKey($key); As i know that the above function remove product one by one based on the key and that key is product id, My Question is Can we remove ...
6
votes
1answer
8k views

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

How can I get "Monthly Best selling product" in Magento 2?
6
votes
4answers
23k views

How to get Product name in phtml file in magento2

I want to get Product name in Phtml file and i checked this three collection but in that there are no product name available. $productCollection = $objectManager->create('Magento\Catalog\Model\...
6
votes
2answers
4k views

Array of product ids for a given category (respecting is_anchor)

I am trying to find the fastest way of getting a list/array of product id's for any given category id. I understand this is quite fast $category = new Varien_Object(array('id'=>$_cat)); $positions ...
6
votes
2answers
8k views

Use final price in addFieldToFilter

I am using this code to filter products by min and max price updated code $collection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('*') ...
6
votes
1answer
4k views

Get item at nth index from a collection

Is there a way to get an item at the nth index from a collection without necessarily loop over all the collection?
6
votes
2answers
8k views

How to filter product collection by matching string like search in Magento 2?

We have loaded product collection based on category ID, we are further trying to filter the product collection result with query string. We have tried : Query string set and get is working fine ...
6
votes
1answer
1k views

Order by joined custom table

I've got a productcollection, which i joined a custom table to. I'd now like to order the collection on the custom joined table row "custom_row_value". I've tried the below method, but this does not ...
6
votes
2answers
967 views

Product collection by non-existing images

I want to create a collection of all the products without a image and set them do the status "disabled". Because i use a automatic import script there are some products that HAD images before, but ...
6
votes
3answers
2k views

Customise price in product collection Magento 2

I am working on Magento 2 and having issue with price. I want to customise the product price. I did it via plugin and it's working fine on product page and list page. As we are using a plugin, it is ...
6
votes
1answer
881 views

How to sorting collection in magento by newest first and out of stock last in same collection

i need to sorting collection in two way in one collection. first i need to sort new product on top. out of stock product in last. basically i need this solution by observer. I used this ... my ...
5
votes
6answers
20k views

Magento2 - product collection filter out of stock products

I could not find any relevant reference how to filter out products that are "Out of Stock" when dealing with product collection. Here is the code sample: <?php $collection = $this->...
5
votes
3answers
3k views

How can I load the product collection without some products?

Actually, I want to load the product collection except for some products. I have the product ids that I don't want in the collection. For example, the product ids that I don't want in the product ...
5
votes
3answers
10k views

How to get only out of stock products in magento 2

How to get out of stock product collection using object manager. I retrieve product collection $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productCollection = $...
5
votes
2answers
7k views

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

For example, I want collection list in order "sku2,sku3,sku1", it is not simple DESC or ASC order. How to achieve it? Example codes is below: $skus = 'sku2,sku3,sku1'; $collection->...
5
votes
2answers
5k views

Get a Magento product collection without sub-products of configurable products

I want to get a list of all products available in my Magento installation. Currently I am using the following call to filter out enabled products and limit the result set to those products which are ...
5
votes
1answer
3k views

Filter products by website using AND

In a multistore setup, I need to find products that are assigned to website A and website B. The product collection has a addWebsiteFilter() method but unfortunately it doesn't serve this purpose, as ...
5
votes
3answers
1k views

Magento 2 catalog filter issue in frontend

I have filter catalog product collection based customer logged in frontend, Using event observer catalog_block_product_list_collection, Filter working fine. But the issue is found in Layered ...
5
votes
2answers
2k views

How to get all values of a given custom attribute?

I have a custom product attribute test_id which is a text field. I want to get all the values assigned to this attribute within a given product collection. I found the following way of doing it. But ...
5
votes
2answers
3k views

Load visible and invisible products at once?

This is my code: $visibility = array( Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, ...
5
votes
2answers
2k views

Why Magento addAttributeToSelect Never Works

Ok I have faced this issue in magento 1 too and I am not able to understand why addAttributeToSelect(*) never works. When I call my product collection I want the collection will all the possible ...
5
votes
2answers
475 views

filtering product collection does not work with command

I face a problem with a collection. I have fresh products that need to be taken out from store 3 days before the expiration date. I created a command that is called by a cron. I've constructed it like ...

1
2 3 4 5
14