Questions tagged [collection]

Questions related to Magento collections

Filter by
Sorted by
Tagged with
8
votes
2answers
818 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\...
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 ...
8
votes
2answers
5k views

addAttributeToSelect not working with core/resource_iterator?

public function run() { $products = Mage::getModel('catalog/product') ->getCollection() ->addFinalPrice() ->addAttributeToSelect('name') Mage::...
8
votes
2answers
8k views

How to get collection of orders which can be shipped?

I need orders which are not shipped yet or are partially shipped. I am using below code to get orders which can be shipped. foreach ($orderIds as $orderId) { $order = Mage::getModel('sales/order')...
8
votes
1answer
2k views

What is the difference between `joinField` and `joinTable` in collections?

In the file app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php, there is a function named joinField and there is another joinTable. If fields are actually columns in the flat tables, what ...
8
votes
2answers
521 views

What is the Magento ORM equivalent of “SELECT DISTINCT attribute FROM products”?

I need to retrieve a list of all values which are used for a specific product attribute, in (pseudo) SQL: SELECT DISTINCT attribute FROM products; How would I use the Magento ORM to generate an ...
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
2answers
7k views

Magento 2: Walk through collection and delete

I was wondering if there was a way of walking through a collection on Magento 2 and deleting entries from the database. In Magento 1, this could be done by doing something like the following: Mage::...
7
votes
3answers
28k 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
5answers
35k 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('*') ...
7
votes
4answers
17k 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?
7
votes
3answers
13k views

Magento2: Load Model Data By Another Field

I have below table named "messages". id | posts_id | message_description | created_at ----------------------------------------------------------------------------- 1 1 test1 ...
7
votes
4answers
12k views

which are best method to get collection in magento 2

I am creating a custom module. which is preferred method to get collection. i need to know the advantage and disadvantage these method? objectManager $objectManager = \Magento\Framework\App\...
7
votes
1answer
44k views

Custom Sort Order For Collection

I need to sort a collection based on "sort_order" field instead of "id" as shown in the image. $collection=Mage::getResourceModel('banners/manage_collection'); $collection->getSelect()->order('...
7
votes
3answers
5k views

Reload collection

Is there any chance to reload collection in Magento 2? For example: foreach($items as $singleItems){ $collection = $this->_myCollection->addFieldToFilter('filed', $singleItems->something); ... ...
7
votes
3answers
18k 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); $...
7
votes
4answers
11k 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
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 ...
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 ...
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
1k views

Filter a collection by NOT FIND_IN_SET

I need to filter a collection by a negative FIND_IN_SET. I know I can do this: $collection->addAttributeToFilter( 'attribute_code', [['finset' => 4], ['null' => true]] ); This ...
7
votes
3answers
6k views

How can I use UNION for 2 collections ? Or merge collections ? (for use in adminhtml grid)

First of all, what I'm trying to do : Get a collection with customers who does not have orders yet and newletters subscribers who dot not have customers account. In mysql FULL JOIN and FULL OUTER ...
7
votes
2answers
1k 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
824 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
838 views

Magento: Filter huge sales order collection

I am trying to filter a huge collection of sales order. There are more then 5 million records in it. Below is my code to get the order collection and also i puts some joins to get the desired columns ...
7
votes
1answer
919 views

Filtering category collection producing incorrect behaviour with flat catalog enabled

I have some code to perform a basic search of categories. It works by searching the category name and description - attributes that are both core. The code I have is as follows: $_categories = Mage:...
7
votes
2answers
2k views

Magento 2 layer navigation custom rating filter pagination doesn't work

I have created a module for rating filter and its working but after filter number of item and pagination showing wrong as per attached screenshot. There is only one 5 start product but showing wrong ...
7
votes
5answers
9k views

Get product collection by root category and all its subcategories in Magento 2?

How can I retrieve a product collection by a root category and from all its subcategories? Eg: Root Category (2 products) Subcategory 1 (2 products) Subcategory 2 (3 products) So I want to ...
6
votes
4answers
38k 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
11k views

How to filter collection by date range on Magento 2?

I want to filter collection according : start date < today end date > today I try this but my getSlides() function return empty results <?php namespace Vendor\Module\Block; use Magento\...
6
votes
4answers
8k views

How to get product collections by set of ids in magento 2?

I wanna something like "IN CLAUSE" in MYSQL using by magento2 ORM, something like this: select * from product where id in (...) but by using collection in magento 2? for example something like this:...
6
votes
1answer
6k views

Magento 2 use group by in collection

I've got a collection from the factory method getCollection(). The problem is that when I use get_class_methods($class) to list all the methods that the collection possesses I can't seem to find one ...
6
votes
1answer
7k views

applying layered navigation to CMS page filled with new products

Can't seem to get it to show up. This is how it's defined in layout files: <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/> it's ...
6
votes
1answer
8k views

Check if product belongs to category?

I have a collection/array of ~4000 products(search results) and a category number and need to filter them. What would be the most efficient way of checking whether a product belongs to a certain ...
6
votes
3answers
6k views

Reload collection after count()

I'm working with a Magento flat collection I want to do this: $collection = 'load a certain collection'; $collection->'addsomefilters'; if (count($collection) == 1) { //add some additional ...
6
votes
1answer
9k views

How to join product collection of magento 2 to a custom table?

I have a custom table user_product_table with a field sku(this field join to product collection) and user. How can I join this table with product collection, so that I don't have to loop my custom ...
6
votes
2answers
6k 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....
6
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?
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
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 ...
6
votes
2answers
6k 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
2answers
19k views

get order status from order id, email and customer's last name

I'm currently looking for a method from which I can get the order status based on the order id, customer email and customer last name. I'd written this code so far public function orderHistoryAction(...
6
votes
3answers
734 views

Find the Grand Total Number of Orders Placed?

I wish to display the number of Orders placed through the website on the Home Page, irrelevant of success or failure. I found a similar question, but not too sure if it's the correct way to solve my ...
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->...
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
4answers
4k views

How to solve admin grid error?

I am getting this error in admin custom module of Magento 2. Something went wrong with processing the default view and we have restored the filter to its original state. admin grid error.
6
votes
1answer
479 views

When do I have to use load() on collection

I can meet many different approaches when using collections in Magento 2. Rarely, but sometimes I see that functions load() is used. Although without it there is no problem with accessing collection ...