Questions tagged [collection]
Questions related to Magento collections
217
questions
7
votes
5
answers
12k
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
4
answers
44k
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
3
answers
12k
views
How to get configurable attribute label and value in magento 2?
To use Object Manager is not a proper way but this code is for testing purpose only.
I want to get attribute value which is available in configurable products. I have tried with below code, but I get ...
6
votes
3
answers
5k
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->...
6
votes
1
answer
9k
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
3
answers
5k
views
How to get disable product collection whenever flat enable at frontend
In my system flat setting has been enable for products and as per as magento system magento exclude disable products from product collection whenever flat table is creating. That why we can not get ...
4
votes
3
answers
2k
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 ...
4
votes
1
answer
3k
views
Magento2 - How to filter product collection on category view
I'm trying to add filters to a product collection on the category page.
I wanted to create an observer that listens to catalog_product_collection_load_before, but I found out that the even is not ...
4
votes
2
answers
2k
views
Search a comma separated string within an ->addAttributeToFilter()
I am using a content management plugin that lets you create posts and assign them related categories.
My issue is that if I create a post that is relevant to two categories then the realted ...
4
votes
2
answers
2k
views
Filter category collection with nested OR/AND statements
So I want to filter a category collection with two attributes. The first attribute attribute_1 must be like a given value. The other attribute may either be like another given value or empty (NULL).
...
4
votes
3
answers
10k
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
8
answers
2k
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
1
answer
3k
views
Filtering error when using custom Varien object collection in admin grid
I have created a custom Varien object collection for my admin module (because the data is not from a db) and have got it successfully to show in an admin grid. I built the collection by using the ...
3
votes
1
answer
3k
views
How to retrieve a customer by phone number?
I need to retrieve the customer object belonging to a certain (mobile) phone number. How can I do this in Magento?
And how can I get a customer by phone number when a phone number was entered by end ...
3
votes
2
answers
8k
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
1
answer
6k
views
Get order collection last one year month wise
I need to get order collection of last one year, In which I have to show data month wise like
Jan - xxx(Orders)
Feb - xxx(Orders)
2
votes
3
answers
9k
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 ...
2
votes
2
answers
2k
views
Magento 2: How to get Customer collection filter by Full name like admin grid?
How to get Customer collection filter by Full name or by keyword like admin grid of magento 2?
2
votes
1
answer
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 ...
2
votes
3
answers
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 ...
2
votes
2
answers
3k
views
How do I get the ID of a dropdown attribute by its text label?
I have a product attribute which is configured to be a dropdown with a fixed list of available values.
Now I need to write a script which will filter based on the selected value of the dropdown ...
2
votes
1
answer
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>
<...
1
vote
3
answers
1k
views
Magento 2 Add new column in Customer Page from data from database
I would like to ask if there is an effective way to add column in Customer's Page Grid in magento admin. I have viewed all tutorials and same problems in the web but no luck on displaying the data. I ...
0
votes
2
answers
3k
views
Filter orders by specific product sku
I need to get order collection, Those orders have a specific product sku,
Like I have a series of sku's "NT", So get all orders which are starting with "NT"
$SKU = NP;
$orders = array();
$collection =...
0
votes
1
answer
11k
views
catalog_product_collection_load_before and getLoadedProductCollection
I have a strange problem, which gives me the feeling i missed some concept:
In an observer triggered via event dispatch <catalog_product_collection_load_before>
i fetch the collection and do a ...
0
votes
1
answer
678
views
addFieldToFilter doesn't return the right product for selected attribute values
I am currently programming a product configurator module for Magento. Everything is working fine so far besides the core functionality: choosing the right product from the database for the selected ...
0
votes
6
answers
2k
views
How can I get the metadescription of every product?
I have made a new Controller and inside there a new Action in which I want to be able to get the metadescription of every product. My code snippet is the following:
public function ...
20
votes
4
answers
27k
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 ...
17
votes
2
answers
30k
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?
16
votes
2
answers
7k
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....
14
votes
5
answers
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
1
answer
29k
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\...
13
votes
1
answer
32k
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()-&...
11
votes
4
answers
25k
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 ...
10
votes
3
answers
3k
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 ...
10
votes
3
answers
2k
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
2
answers
13k
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::...
9
votes
5
answers
6k
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
1
answer
14k
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 ...
8
votes
1
answer
15k
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 ...
8
votes
1
answer
3k
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
6
answers
9k
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.
8
votes
2
answers
2k
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
2
answers
11k
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 ...
8
votes
2
answers
5k
views
addAttributeToSelect not working with core/resource_iterator?
public function run()
{
$products = Mage::getModel('catalog/product')
->getCollection()
->addFinalPrice()
->addAttributeToSelect('name')
Mage::...
8
votes
2
answers
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('*')
->...
7
votes
3
answers
8k
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
2
answers
3k
views
Magento2.2 getCollection from Factory deprecated
I get in my editor (PhPStorm) on getCollection the message that it is deprecated.
In my controller i got the function
public function __construct(
Registry $registry,
...
7
votes
2
answers
2k
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
1
answer
2k
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 ...