magento getCollection() can we put where condition? - magento

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)));

Related

Multiselect attribute get collection of product

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))

complex boolean conditions on Magento

I cannot find the solution to this problem in magento tutorials.
How can I implement the boolean OR operator in custom models when there are two attributes involved? The example in the official tutorial only demonstrated the use of OR boolean for one field, sku.
$filter_a = array('like'=>'a%');
$filter_b = array('like'=>'b%');
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('sku', array($filter_a, $filter_b))
->getSelect();
This translates to
WHERE e.sku like 'a%' or e.sku like 'b%'
But what if I need to run conditions such as :
WHERE (e.sku like 'a%' or e.sku like 'b%') or (table_price.value >= '10' )
How can I do this on Magento? Thanks
You have your syntax incorrec, try this:
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter(
array(
array('attribute'=>'firstname', 'like'=>'test%'),
array('attribute'=>'lastname', 'like'=>'test%'),
)
)
you could replace the attribute names by 'sku' or what ever you wanted. Each array entry will be OR'd.

How to get products that are found in 2 or more categories

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!

Joining AND conditions with OR condition on Magento

I am able to run AND and OR boolean conditions on Magento using addFieldToFilter and addAttributeToFilter.
But, I am curious about implementing more complex boolean conditions like below:
WHERE (`sku > 5` AND `price` > '10') OR (`name` = 'books')
for AND condition, I have
$collections = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addFieldToFilter( 'sku', array('gt' => 5 )
->addFieldToFilter('price', array('gt' => 10);
Which translates to the first parenthesis of the WHERE condition:
(`sku` > 5 AND `price` > 10)
I do not know how to proceed from here to express the OR condition
`name` = 'books'
Any help will be appreciated. Thanks
See my earlier post:
complex boolean conditions on Magento
You can create OR by passing an array:
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter('price', array('gt' => 10))
->addAttributeToFilter(
array(
array('attribute'=>'firstname', 'like'=>'test%'),
array('attribute'=>'lastname', 'like'=>'test%'),
)
)
Each additional call to addAttributeToFilter() will ANDed, but firstname / lastname will be ORed.
If you need more complex select statements you can always get the select object from the collection and perform adjustments to the query. Look at Varien_Db_Select for hints :)
$collection->getSelect()->join(..)->orWhere(..); // etc
You can even debug the query to see if it's looking ok:
echo $collection->getSelect()->__toString();

Magento Filter By Relative Value

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);

Resources