Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [collection]

Questions related to Magento collections

15
votes
4answers
18k views

Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue

I'm adding a column to the order grid using observer approach: On the event -> sales_order_grid_collection_load_before I'm adding a join to the collection On the event -> ...
77
votes
5answers
65k views

Difference between getSize() and count() on collection

I have heard many times that they both are the same. But I am facing a weird issue, in the product collection of CatalogSearch module, count() is returning correct product count while getSize() is ...
11
votes
4answers
11k views

To add default filter to grid in Magento2?

I have created one custom module with title & status field attribute in admin grid. Grid collections in module_test_grid_block.xml <arguments> <argument name="id" ...
42
votes
5answers
110k views

How to get all items in cart currently?

I want to get a collection of all the items in cart currently. How can I do this ? Any suggestions will be appreciated.
18
votes
7answers
97k 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 ...
17
votes
2answers
46k views

addFilter vs addFieldToFilter

Magento collection has two methods for filtering: 1. Varien_Data_Collection_Db::addFieldToFilter 2. Varien_Data_Collection::addFilter Seems that both methods add where condition to Zend_Db_Select. ...
7
votes
2answers
6k views

layered navigation isn't populated by collection attributes — CMS page

I'm attempting to add layered navigation to a CMS page containing a widget that contains a custom product collection (products that are 'new'). Within this widget are fuctions that, given the correct ...
7
votes
5answers
34k views

Filter product collection by multiple categories?

I have this snippet of code that gets the most recently added products: $_productCollection = Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('*') ...
15
votes
1answer
13k views

how to add pagination on custom collection in Magento 2

I am working on custom module. How can i use default magento pagination with my custom collection and also set custom limits on that?
4
votes
2answers
994 views

Programatically get subcategories of several root categories with collection

I am working on a collection query of categories: $categories_check = Mage::getModel('catalog/category') ->getCollection() ->setStoreId($storeId) ->...
7
votes
3answers
17k views

How to add dynamic field in magento collection?

I have created one magento collection like this to calculate distance from lat long. $collection = Mage::getModel('module/module')->getCollection()->addFieldToFilter('status',1); $...
18
votes
2answers
71k views

How to get attribute “option label/attribute text” having “attribute value” (option_id)?

Suppose I have an attribute that is a collection of option (dropdown/multiselect). I can retrieve the attribute value for a given product: $store_id = [something]; $productId = [something]; // this ...
7
votes
3answers
27k 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
3answers
4k views

How to update category page with custom filters, and also layered navigation, Magento 2.1

I need to update category page collection by filtering restricted SKUs. As like $collection->getSelect()->where("`e`.`sku` IN('24-MB04', '24-MB03', '24-MB02')"); Above query gives collection ...
16
votes
3answers
26k views

Filtering results using LIKE

Consider these three "haystack" strings: a) foo bar b) welcome to foo bar industries c) foo barer And now my "needle": foo bar (Heh) I would like my filter to match my needle with haystack ...
5
votes
2answers
5k views

magento2 search_tmp join leads to empty results in category listing

I just imported products into fresh magento2 instance and encountered issue that category product listing block are empty, although the products successfully appear in database . I have debugged list....
3
votes
2answers
3k views

Magento 2 - Layered Navigation for Custom Product Collection

I created a CMS page for products that contain a certain attribute (On Sale Items). I extended the ListProduct class and added filters to the product collection to achieve the desired results. (...
21
votes
2answers
9k views

Is it possible to iterate over Magento collections with pagination natively?

What I mean by that is - is there a way to do: $collection = $model->getCollection(); foreach ($collection as $item) { $item->doStuff(); } In such a way that even if the collection had ...
1
vote
1answer
835 views

Magento 1 : Filtering / Paging / Sorting with Varien_Data_Collection

I'm back to this good old problem where using a Varien_Data_Collection in a grid does not handle sorting, paging nor filtering. I've found some useful resources such as : https://www.maximehuran.fr/...
18
votes
4answers
4k views

Issue using “having” in Magento collection

I am trying to build a custom collection for a grid in Magento admin module. I have created a new collection method called "addAttributeHaving" which just does the following: public function ...
8
votes
2answers
14k views

Collections - Filtering multiple multi-select attribute values

That's not a typo. I am aware that I need to use 'finset' to filter my multi-select attributes; however, I am trying to filter multiple values at once and getting: Incorrect parameter count in the ...
27
votes
2answers
67k views

Left join a table on a collection query

I am doing the following to get some orders out of the system for export: $orders = Mage::getModel('sales/order')->getCollection() ->addFieldToFilter('status', $statusToExport) ->...
22
votes
3answers
10k views

How to truncate magento table using collection?

I have a custom table. I want to truncate the table using Magento collection without SQL query. Hope someone will provide some useful information.
12
votes
2answers
6k views

How to clone the collection in Magento?

I have one collection in a method where I want to perform two different operations on this collection. So, I want two separate copies of same collection, and then assign one of the two collections to ...
6
votes
1answer
3k views

How to correctly select the first item from a filtered collection?

I'm trying to write a script to get a category given its path, which seems like it should be easy to do. As a test, I have a base category with an ID of 2, and it has 2 children, 'Foo' and 'Bar'. I'm ...
12
votes
3answers
7k views

Layered navigation for custom collection on custom page - magento2

I am working on fetching layered navigation in magento2 for a custom product collection. I am getting custom collection already on the custom page need to show layered navigation. Tried to adapt this ...
7
votes
4answers
10k views

How to add item to Magento collection to the beginning?

It is possible to add element to the collection as the first element, similar to this method which adds element to the end? $product = Mage::getModel('catalog/product')->load(15879); $...
7
votes
1answer
1k views

Product loop giving incorrect images

I am looping through products and getting an error with product images. The code I am working with is below: <?php $collection = Mage::getModel('catalog/product')->getCollection(); ...
3
votes
3answers
2k 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 ...
9
votes
5answers
15k views

How to log SQL query of the collection?

I want to log the SQL query for a custom collection. I tried the following code. But did not work. Any suggestions will be appreciated. Mage::log($collection->getSelect(),null,'test.log',true);
6
votes
2answers
6k views

Magento join query with group by and having error

join query with customer table, and group by and having clauses not working, its show an error , filed is not found. The Query $collection = $this->getCollection(); $collection->...
4
votes
2answers
358 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, ...
3
votes
1answer
51 views

Complexe filter in a collection

I would like to use addFieldToFilter (or other...) in a collection to make this filter : (item1 = 'value' AND (item2 = 'value1' OR item = 'value2')) OR (item2 = 'value3' OR item = 'value4')
2
votes
1answer
426 views

What is the equivalent of the config node category/collection/attributes in Magento 2 to add an attribute to the category collection

I am porting a Magento 1 module, which adds a custom category attribute, to Magento 2. The product is already created in Magento 2 - but now it does not appear in the inside \Magento\Catalog\Helper\...
29
votes
1answer
27k views

About Collection Model and Resource Model

I am bit confused about using resource model and collection model. Sometimes when I see examples for loading products using specific attributes, in some examples, they use collection model and ...
12
votes
1answer
18k views

How to specify custom sort order for product collection?

I am trying to create a product collection based on array of products ids, and also sort the collection based on the ids array. $productIds = array(318,310,311); $collection = Mage::getModel('catalog/...
10
votes
4answers
17k views

Magento 2: How to Filter a Product Collection By Store ID

Using a product factory object, I'm able to create a product, grab a product collection, and fetch the first item of that collection /* var $productFactory \Magento\Catalog\Model\ProductFactory */ $...
2
votes
3answers
7k views

Magento: Hiding and not counting out of stock in layered navigation

About: Hiding and not counting out of stock in layered navigation in Magento Hi, this has boggled us for quite a while. An I did not find a solution. We upgraded to Mage 1.8.1 hoping that it would be ...
6
votes
4answers
37k 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 ...
5
votes
1answer
21k views

Magento join collection query

I have this line: $collection->getSelect()->join( 'sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id', array('telephone','city','postcode','...
2
votes
1answer
2k views

Can not override Sales Resource Model Collection class

I am trying to override Mage_Sales_Model_Resource_Order_Collection My custom module's config: <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <...
7
votes
1answer
4k 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 ...
5
votes
1answer
5k 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?
4
votes
3answers
9k views

How to get last created product id?

I have overridden the Mage_ImportExport_Model_Import_Entity_Abstract class and in the function _saveValidatedBunches() I want to get the last created product Id. I've tried the following, but it ...
4
votes
3answers
784 views

What is faster - getting raw attribute value or use collection?

I need to load one attribute value(weight) for many product IDs. Say list of the product ids is in array $productIDs. In general case, which approach is faster, Loading raw attribute value for ...
3
votes
2answers
7k views

Create magento pagination in custom product collection inside phtml file

First of all, i didn't create custom module to handle this product collection, so, i just put model inside phtml file, i know it's bad implementation, i'm new in magento and i have to do small ...
2
votes
2answers
3k views

Magento 2: how to use the collection walk iterator?

Back in Magento 1, it was possible to use the collection iterator to walk through the results and avoid looping through them. It was a huge improvement in terms of performance when dealing with ...
2
votes
3answers
17k views

Magento - Get Products from Specific Category

I am currently using the following code to display four products in a random order. It works, but I'd like to limit the results to a particular category or an array of categories. How would I go about ...