Questions tagged [product-collection]

Questions related to Magento product collections.

Filter by
Sorted by
Tagged with
18
votes
7answers
102k 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) ->...
7
votes
3answers
9k 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 ...
24
votes
3answers
14k 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->...
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 ...
4
votes
1answer
2k views

Custom sorting for product collection

I have to implement the following scenario. The default sort order in category (by position) should ignore the position set for the products in the category. Instead when sorting by position it should ...
4
votes
6answers
17k 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->...
3
votes
3answers
3k views

Random products from same category with parent category fill up

I would like to do following on 1.9.0.1: Getting 12 random upsell-products from same category as current product. They have to be saleable. They shouldnt be in related-products already. They shouldnt ...
3
votes
3answers
20k views

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

I trying to join custom table with product collection but I am not getting result. But printed SQL query is running fine namespace Vendor\Module\Block\Product; use Magento\Catalog\Api\...
2
votes
4answers
4k views

How to get recently viewed products collection of Customer Magento 2?

Magento 2 get recently viewed products collection of Customer (Using customer ID) programmatically I tried this solution But I want recently viewed products collection. I tried below solution too ...
4
votes
2answers
418 views

Magento getting store bestsellers: configurable products, only from current store

Magento getting store bestsellers: configurable products, only from current store We have tested some bestseller codes like from here and here and here. They all work fine if you only sell simple ...
4
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, ...
1
vote
4answers
720 views

Why does getSize() does not match cart collection size?

Question popped up here: Filter collection on checkout/cart Why does a filtered $collection->getSize() returns correct value, but iterating over collection has more items? three items are in cart ...
10
votes
1answer
16k 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::...
6
votes
4answers
41k 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
1answer
7k 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?
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 ...
3
votes
3answers
7k 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 = $...
3
votes
8answers
416 views

Magento2: I want to show featured product on top of the category and search result

I have yes/no type product attribute. If this featured attribute is yes for any products, those product will show top(first featured product showing in category page then rest of the products are ...
3
votes
1answer
824 views

Find MIN & MAX value of product attribute in a large category

How do I find the max weight and min weight of a category with 3000 products. I'm trying use: public function getProductCollectionFilter($_productCollection) { $_productCollection = $this->...
2
votes
2answers
5k views

How to check if a product is already in the cart

I have this block of code which displays product from a certain category in the cart for last minute buys. <?php $categoryid = 1103; $category = new Mage_Catalog_Model_Category(); $category->...
2
votes
1answer
10k views

How to add date filter to product collection

I was wondering if there is any way to add a date filter to a product collection. What i am trying to do: I have a collection for new products, i need to add a filter to it so it filter the products ...
1
vote
2answers
7k views

Show New Products in a new page

have created a 'new page' as 'latest products' and i want to show all the products in this page but sorted 'datewise' i.e. new first .. i got from google was to use this code <reference name="...
1
vote
3answers
678 views

Add WHERE with IN and OR to a product collection

I want to add something like WHERE (table1.attrib1 IN (1,2,3) OR table2.attrib2 IN (1,2,3)) to a product collection to which I have already joined a couple of tables. I tried this, but it doesn't ...
0
votes
1answer
676 views

Avoid duplicates in random collection? [duplicate]

My category list is showing a random list of products, but as one scrolls down the page it shows duplicates because it is just takes into account the first 20, as set by setPageSize. $...
8
votes
3answers
9k views

Get product collection with product ids

Is it possible to get a product collection bassd on array of product ids?
9
votes
3answers
2k 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 ...
23
votes
6answers
53k 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: ...
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 ...
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('*') ->...
6
votes
4answers
18k 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\...
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 ...
9
votes
4answers
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')->...
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
2answers
2k views

Problem with addWebsiteFilter and addStoreFilter

I am working on my product collection query. I used "addWebsiteFilter" and "addStoreFilter" in my collection query. The problem is addWebsiteFilter supports array of website ids but addStoreFilter not ...
5
votes
4answers
53k 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 ...
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 ...
4
votes
3answers
1k views

“random” order clause added to my collection

For a module I load a custom product collection where I select products based on IDs in an array, I also try and sort the collection by the same order as the IDs have in the array(). To achieve the ...
3
votes
1answer
2k views

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

I am trying to get all products based on catalog price rules and cart price rules under all categories which is valid for today's date in Magento 2
3
votes
2answers
5k 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->...
3
votes
1answer
3k views

$product->getFinalPrice() and $product->isSaleable() are very slow when called from a product collection

Loading around 400 products without using $product->getFinalPrice() and $product->isSaleable() usually takes around 2 seconds. When calling them, however, the load times shoot up to around 12 ...
1
vote
1answer
362 views

Need help with display random products code

I wanted to generate random products for certain categories on my home page. Came across this code: <?php // $_productCollection = $this->getLoadedProductCollection(); $_helper = $this->...
0
votes
2answers
1k views

Sort Product Collection by custom integer attribute

I created a custom_stock_status attribute in my module with 'int' backend type and four options in resource. array( 4 => 'In Stock', 3 => 'Soon', 2 => 'Out Of Stock', 1 => 'Discontinued', ...
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 ...
5
votes
10answers
50k 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
5
votes
3answers
2k views

Incorrect product count in layered navigation after overriding list product collection

I have customized(override) product list collection getProductCollection and filtered collection as required. public function getProductCollection() { if (isset($this->...
5
votes
0answers
2k views

Proper way to get product collection with store specific values in admin area

For a custom indexer, I need to load a product collection with the attribute values for a given store, while not in a store context. As it has been established in Magento 2: How to Filter a Product ...
4
votes
2answers
1k views

Raw sql for product collection with addAttributeForSelect() method without loading collection

How can I get raw full sql query from non-loaded product collection with addAttributeToSelect() method without load collection ? This code: $collection = Mage::getModel('catalog/product')->...
3
votes
1answer
458 views

Magento2 - catalog product listing collection sort by price with dynamic price calculation

product price is calculated dynamically based on certain conditions. while sorting with price it takes only default price not dynamically calculated price. the same issue with the price filter. I can ...
3
votes
2answers
6k views

Using Factory get product collection with product name in Magento 2

I get product collection using \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in block( dependency injection), the result doesn't have name attribute, The similar Q&A(...
3
votes
1answer
3k views

Get product id of current category page [closed]

I listen to this event: catalog_product_load_after and I need to retrieve the products id, I know I can retrieve the products' id with: $collection = Mage::registry('current_category') ...