I have a multiselect attribute "my_attribute" with data
slot_1
slot_2
slot_3
slot_4
and, products with assigned attribute eg.
product_1 - slot_1 AND slot_3
product_2 - slot_3 AND slot_1
product_3 - slot_4 AND slot_2
product_4 - slot_1 AND slot_4
...
I would try show only product witch contains slot_1 and slot_3, In the result I should get
product_1 and product_2 I was try accomplish this by:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('attribute ',array('in' => array(slot_1,slot_3)))
but I'm get unexpected result
I read a article from this post but i can't find the solution.
I'm wondering how i can solve my problem
thx for help
try
addAttributeToFilter('slot_1', array('eq' => slot_3))
Related
I have been trying for days now to get a clean solution on how to filter a product collection by 2 or more categories. The products should be in both categoryA and categoryB, not in any of them.
I have tried multiple solution that I have found on the internet but with no success. The only workaround I have found so far (but I’m not found of it) is using a raw query and then get the collection from the ids:
SELECT e.entity_id FROM catalog_product_entity AS e INNER JOIN catalog_category_product AS ccp on ccp.product_id=e.entity_id where ccp.category_id =100 or ccp.category_id = 101 group by entity_id having count(*) > 1
This would return the list of products that are found in both category 100 and 101.
However, I hope there is actually a “Magento way” or doing this without needing to execute raw queries. Anyone have clue on this?
Thanks
I finally managed to find a proper solution after a long night of work:
$cat_ids = array(4,5,6);
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => array('finset' => $cat_ids)))
->getSelect()
->group('entity_id')
->having('count(*) = ' . count($cat_ids));
There yo go. The above will return all products that are simultaneously part of all provided categories. Thanks to activeDev and Jurgen for the help.
This should work fine.
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => array('finset' => '36,37')))
->addAttributeToSelect('*')
->setPageSize(5);
This comes from http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/, but seems slightly out of date.
Im not able to confirm if this works as im working on a version that currently contains no categories. but i am getting an empty collection back as expected, so let me know!
I'm attempting to build a product reviews collection that combines reviews from multiple different products. I'm using the following code, but the collection only returns the reviews from the first product ID listed in the array, instead of both product IDs.
$entity_ids = "153435, 153438";
$reviewcollection = Mage::getModel('review/review')->getCollection()->addEntityFilter('product', array('in' => $entity_ids));
$reviewcollection->addStoreFilter(Mage::app()->getStore()->getId())->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)->setDateOrder()->addRateVotes();
$_items = $reviewcollection->getItems();`
Any ideas how to get the collection to combine the reviews from multiple product IDs?
$entity_ids = array(153435, 153438);
$reviewcollection = Mage::getModel('review/review')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->addFieldToFilter('entity_id', Mage_Review_Model_Review::ENTITY_PRODUCT)
->addFieldToFilter('entity_pk_value', array('in' => $entity_ids))
->setDateOrder()
->addRateVotes()
;
$_items = $reviewcollection->getItems();
addEntityFilter() doesn't accept an array. You could try addFieldToFilter($field, $condition) or override the collection and write your own method. Alternatively, you could just loop through your ids and merge the results.
I would like to filter a collection by relative value per that row. For example,
SELECT * FROM table WHERE column_1 > column_2
The only thing I know how to do in Magento would be
$q = Mage::getModel('table')->getCollection()
->addAttributeToFilter('column_1', array('gt' => $some_number));
or something of that sort. I can only give it a value to compare against, not a column name. I also looked at the Zend_Db_Select method at the where clause but didn't find anything that would help. Do I actually have to go all the way down to a direct SQL query (something which is, of course, avoided at all costs)? (I'm running Magento 1.3.2.4)
Thank you.
Try something like
$q = Mage::getModel('table')->getCollection()
->addAttributeToSelect('column_1')
->addAttributeToSelect('column_2')
->addAttributeToFilter('column_1', array('gt' => Zend_Db_Expr('`column_2`')));
OK, this is probably not the ideal solution, but it's one way of getting what you need.
This is how I got a collection of products that were on sale, where a products final price is lower than its price.
I first made a new empty data collection. Then defined the collection I was going to loop through filtering what I could first. After that I looped through that collection and any products that matched my requirements got added to the empty collection.
$this->_itemCollection = new Varien_Data_Collection();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addCategoryFilter($category)
->addAttributeToSort('position', 'asc');
$i=0;
foreach($collection as $product){
if($product->getFinalPrice() > $product->getPrice() && !$this->_itemCollection->getItemById($product->getId())){
$this->_itemCollection->addItem($product);
$i++;
}
if($i==$this->getProductsCount()){
break;
}
}
$this->setProductCollection($this->_itemCollection);
When getting a product collection in Magento, I would expect the StoreFilter to do just that, filter by the current store. But I can't get it to work.
Say I have 2 stores set up like so:
And both stores have a different root category. Main Store is the default sample data, Store2 has just one product I added. I would have thought that using the store filter, only products within the root category of the current store would show up. But I'm getting every product showing. I'm testing this by placing the following in my category view template:
$store_id = Mage::app()->getStore()->getId();
$_testproductCollection = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($store_id)
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName());
};
If I echo the store ID, it's giving me the correct number. I have only one product in Store 2, so why am I getting every product from all stores returned? I can set every product in Main Store to not show in Store2 and then add a visibility filter, but that would take forever.
Also, I just noticed, if I echo the products store ID, I get the current ID, not the store it's assigned to:
echo $_testproduct->getStoreId()
What's going on?
UPDATE (April 8 2011):
OK, so I tried joining the fields so that the store_id is included (as suggested below), the section of code {{table}}.store_id = 1 is just setting all the products to have a store_id of 1. How can I just get the store_id associated with the product?
$_testproductCollection = Mage::getResourceModel('catalog/product_collection');
$_testproductCollection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
$_testproductCollection->getSelect()->distinct(true);
$_testproductCollection->addAttributeToSelect('*')->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
echo "STORE IS ".$_testproduct->getData('store_id')."<br/>";
};
If I check the catalog_category_product_index table of my db, the store_id's are correct.
$_testproductCollection should look like this $_testproductCollection = Mage::getResourceModel('reports/product_collection')->addAttributeToSelect('*')->addStoreFilter().
If You print SELECT from that collection You will see that there ain't any store column, so addStoreFilter() can't apply WHERE.
You should use joinField() on Your collection and add store_id column from catalog_product_entity_varchar table.
EDIT
Sorry to keep You waiting ;)
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->joinField('store_id', 'catalog_category_product_index', 'store_id', 'product_id=entity_id', '{{table}}.store_id = 1', 'left');
$collection->getSelect()->distinct(true);
This should do the trick, but just to be sure, please check if you're getting right products :)
This worked for me:
Mage::app()->setCurrentStore($storeId);
$productCollection = Mage::getModel('catalog/product')
->getCollection()
->addStoreFilter()
->addAttributeToSelect(array('sku','price'));
OK, I think this works, haven't tested too much but seems to have done the trick. You need to first get your stores root category id, then join some fields so you have access to the products "category_id", then filter using that:
$_rootcatID = Mage::app()->getStore()->getRootCategoryId();
$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left')
->addAttributeToFilter('category_id', array('in' => $_rootcatID))
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName())."<br/>";
};
I think
You don't need to do any joins as the magento's setStoreId() will work.
$collection = Mage::getModel("catalog/product")
->getCollection()
->setStoreId(1) //change store Id according your needs
->addAttributeToSelect(array('name','url','sku'))
->setPageSize(20);
This will get maximum 20 products from store id 1
i am working on a magento project and i need to get the value according to country wise like select address where country ="Nepal"
can we send the where condition in getCollection() function
$collection = Mage::getModel('relocator/location')->getCollection();
any help will be appreciated
got the solution
;P
Mage::getModel('relocator/location')
->getCollection()
->addFilter('country','Nepal');
You can use diffrent condition like below reference from this.
$collection = Mage::getModel('catalog/product')->getCollection();
Is Equal To
$collection->addAttributeToFilter('status', array('eq' => 1));
Greater Than
$collection->addAttributeToFilter('price', array('gt' => 3));
Contains – with % wildcards
Is NULL
$collection->addAttributeToFilter('sku', array('like' => 'DVD%'));
$collection->addAttributeToFilter('entity_id', array('nin' => array(1,2,12)));