Questions tagged [collection]

Questions related to Magento collections

Filter by
Sorted by
Tagged with
83
votes
6answers
82k 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 ...
16
votes
4answers
19k 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 -> ...
13
votes
5answers
20k 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" ...
45
votes
5answers
118k 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.
13
votes
3answers
17k 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 ...
17
votes
1answer
25k 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?
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) ->...
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 ...
19
votes
2answers
65k 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
5answers
36k 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('*') ...
9
votes
2answers
10k 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....
4
votes
2answers
1k 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) ->...
8
votes
2answers
7k 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 ...
8
votes
3answers
19k 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); $...
5
votes
3answers
7k 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 ...
18
votes
2answers
74k 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
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 ...
8
votes
2answers
17k 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 ...
17
votes
3answers
35k 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 ...
21
votes
2answers
11k 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
1k 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/...
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 ...
27
votes
2answers
75k 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) ->...
7
votes
3answers
10k 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. (...
23
votes
3answers
11k 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.
11
votes
4answers
29k 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 */ $...
12
votes
2answers
8k 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 ...
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 ...
13
votes
5answers
61k views

Magento - 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(); $componentQuantityCollection-&...
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(); ...
7
votes
2answers
7k 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->...
7
votes
4answers
13k views

In Magento 1.x How to add item to 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); $...
9
votes
5answers
17k 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);
4
votes
1answer
3k views

Magento2: How to get Cart items collection on Product view page?

I want to get Cart items collection on product page. customer-data.js define([ 'jquery', 'ko', 'uiComponent', 'Magento_Customer/js/customer-data' ], function ($, ko, Component, ...
4
votes
2answers
441 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 ...
3
votes
1answer
1k views

Filtering grid by using filter_url_params is not working

I added a grid to a ui_component form and I want to use an Url-parameter to filter the content of the grid. Herefore I added the 'filter_url_params' in my listing: <dataSource name="...
10
votes
2answers
8k 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 ...
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
614 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\...
3
votes
1answer
53 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')
29
votes
1answer
28k 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 ...
16
votes
2answers
66k 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-&...
12
votes
1answer
19k 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
30k views

Magento 2 how to get order collection

how to get order collection in custom module ? like My Orders collection in customers dashboard (frontend). this collection how to get?
2
votes
3answers
8k 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
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
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?
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
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 ...

1
2 3 4 5