Questions tagged [collection]

Questions related to Magento collections

Filter by
Sorted by
Tagged with
80
votes
6answers
68k 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 ...
44
votes
5answers
111k 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.
37
votes
3answers
28k views

Most efficient way to get all ID's from a collection

In the past to get all ID's of a product collection, I have always used getAllIds on the collection, believing this was a method that prevented the full collection load with data etc. But, I actually ...
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 ...
27
votes
2answers
68k 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) ->...
24
votes
1answer
748 views

Are collections history in Magento 2?

I know that a lot of code that is currently in Magento 2 (2.1.2) is more or less ported from Magento 1 and that a lot of code will be substituted by an equivalent in the future. In this aspect, I'm ...
23
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.
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 ...
20
votes
5answers
5k 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 ...
18
votes
2answers
72k 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 ...
18
votes
7answers
98k 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
2answers
49k 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. ...
18
votes
4answers
24k views

How can I fix “Item (Mage_Catalog_Model_Product) with the same id ”xxx“ already exist”?

I have been getting this error when trying to filter a products collection Item (Mage_Catalog_Model_Product) with the same id "6058" already exist and wanted to ask what could ne causing the error ...
16
votes
3answers
27k 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 ...
16
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 -> ...
16
votes
1answer
14k 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?
15
votes
2answers
24k views

Difference Between setData() and addData()

What's the difference between setData() and addData()? Is there a connection if I update a product or set a new one?
15
votes
4answers
23k views

Magento - custom (non eav) model, load by multiple fields

I have a custom model and resource model. I want to load a single instance of the model using more than 1 field. The model has the following fields: id tag_name custom_name group_name I want to ...
15
votes
2answers
6k views

Why magento use resource and mysql4 model collection?

What is the purpose of using these two models collection. Resource Mysql4 why can't we use resource model for all collections. Why we are using MySql4 collection for simple module Some one explain....
15
votes
3answers
2k views

Efficient Collection Calling, Filtering, and Loading

Right now im reusing a lot of collections that are nested within foreach loops. Is it possible to move these things up a few levels? Currently Im forced to reload collections that have 51k + entities ...
14
votes
5answers
14k views

Filter product collection by non-flat attribute

I am doing the following: $productCollection = Mage::getModel('catalog/product') ->getCollection(); $productCollection ->addAttributeToFilter('my_attribute', 1); my_attribute is not ...
13
votes
1answer
26k views

How to use group by in magento collection with join query

In module's admin grid I am using this code to get collection and group them by customer id $collection = Mage::getModel('referafriend/statistics')->getCollection(); $collection->getSelect()-&...
12
votes
4answers
28k views

How to print collection mysql query in magento 2?

There is getSelect()->__toString(); is available in Magento 1 for print query of collection. like below example $products = Mage::getModel(‘catalog/product’) ->addAttributeToFilter(‘status’, ...
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/...
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 ...
12
votes
3answers
8k 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 ...
11
votes
5answers
52k views

unable to set order of collection

This doesn't seem to be ordered correctly, anything i'm doing wrong? Suggestions? $componentQuantityCollection = Mage::getModel('catalog/product')->getCollection(); $...
11
votes
2answers
22k views

Magento2 Order by clause in collection

I have a custom collection to which I add filters. For example, I have this: $giftColletion = $this->_giftFactory->getCollection(); $giftColletion->addFieldToFilter('store_id', 1); What ...
11
votes
4answers
22k views

Best practice to join product attributes

I have a custom table with a product reference product_id. Now I would like to show product information (sku, name) in my backend grid, but I am unsure what is the best practice to do this? My best ...
11
votes
4answers
12k 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" ...
11
votes
4answers
19k 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 */ $...
10
votes
2answers
36k views

Magento 2 get Parameters in the URL [duplicate]

I have created an admin form with phtml content and a ui component grid. Phtml section working fine. Now I want to filter the grid collection based on the url param. I have used the below code $this-&...
10
votes
3answers
1k views

Magento 1: Performance optimizations to delete entities

I'm currently trying to improve a couple of modules regarding performance. Some of you may know the usage of the walk() method on collection which is very useful to avoid looping through products ...
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);
9
votes
1answer
24k views

How to list all users

As part of an exercise I am trying to list all users email from my database on one page. So far the closest i've got is $user = mage::getModel('customer/customer')->getCollection()->getData(); ...
9
votes
2answers
4k views

Grid pagination not working when use group clause in collection

I m working on product grid but its pagination or product count not working(as it display wrong count). as my block _preparecollection function is as below.i have added category filter code in ...
9
votes
6answers
5k views

magento 2: How to use the order by and limit clause in sql query

I am using following code to filter result and i want to limit the result. $giftColletion = $this->_giftFactory->getCollection(); $giftColletion->addFieldToFilter('store_id', 1); $...
9
votes
2answers
17k views

Magento Filter collection by create times (today, yesterday, week, hour etc)

I have a custom collection that I wish to filter by created date and het entries created "yesterday" Collection Entries //dates are set in controller using setCreatedTime(Mage::getModel('core/date')...
9
votes
1answer
2k views

What is the use of addExpressionFieldToSelect over addFieldToSelect?

I'm studying Magento and I don't really understand what is the use of addExpressionFieldToSelect over addFieldToSelect? I haven't found the answer anywhere in my reading. As far as I can tell, it ...
9
votes
2answers
7k 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: $...
9
votes
1answer
5k views

Whats the best way to check if a custom attribute exists in Magento2

In being careful, I want to check if my custom attribute exists before I attempt to utilize it in any way. I need to check against all attributes collection, not just the product collection. My ...
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')->...
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
915 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
1k 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

Magento 2: Select Field From Collection Query

I have below query. I want to select only 1 field from table instead of all. $collection = $this->_collectionFactory->addFieldToFilter('status', 0)->load(); //$collection->getSelect()->...
8
votes
1answer
4k views

Magento customer grid _prepareCollection() override is not working

I had override _prepareCollection() method of Mage_Adminhtml_Block_Customer_Grid and added following lines ->addAttributeToSelect('cus_city') ->addAttributeToSelect('cus_country') ->...
8
votes
2answers
800 views

Magento 2: different way get field of a collection

I have this helper class in Magento 2: class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $_countryFactory; public function __construct( \Magento\Directory\...