Magento both AND and OR conditions in a collection - magento

I want make a query which looks like below to filter some products (using attributes) from product collection.
SELECT <attributes>
FROM <tables & joins>
WHERE (<some AND conditions>) OR (<some AND conditions>)
WHERE condition should filter products that match either first set of AND conditions or second set of AND conditions.
Problem is I can't find a way to add an OR condition in between multiple AND conditions.
Can anyone help me to code above where condition using Magento addAttributeToFilter()? or any other functions?

If i'm understanding you correctly I think you need to do some variation of this:
->addAttributeToFilter(...filter here...)
->addAttributeToFilter(array(
array(
'attribute' => 'special_to_date',
'date' => true,
'from' => $dateTomorrow
),
array(
'attribute' => 'special_to_date',
'null' => 1
)
));
which would be:
...filter here... AND (special_to_date >= '2012-07-03' OR special_to_date IS NULL)...

Related

Hard Join in Magento

i am a beginner Magento Developer and i have a difficult task to add in a grid a special column.
The column is named Type RMA. I made the sql query but i need to translate it in Magento my query is:
SELECT t1.*, (t3.total_qty_ordered - t2.qty_requested) as 'type_rma'
FROM enterprise_rma_grid as t1
JOIN enterprise_rma_item_entity as t2
ON t1.entity_id = t2.entity_id
JOIN sales_flat_order as t3 ON t1.order_id = t3.entity_id;
So in my _prepareCollection() i have something like these :
$collection = $this->getCollection();
$collection->getSelect()
->join();
And i have no idea how to translate my SQL code in a magento one. My goal is to add the values calculated above as a new column named "type_rma", and display it in my grid as:
$this->addColumn('type_rma', array(
'header' => Mage::helper('enterprise_rma')->__('Type RMA'),
'type' => 'options',
'width' => '100px',
'index' => 'type_rma'
));
Thanks in advance for all your ideas and help.
I found the solution after 10 hours of searching...
$collection->getSelect()
->join(
array('t2' => 'enterprise_rma_item_entity'), 'main_table.entity_id = t2.entity_id',
array('type_rma' => new Zend_Db_Expr("(t3.total_qty_ordered - t2.qty_requested)")
)
)->join(
array('t3' => 'sales_flat_order'), 'main_table.order_id = t3.entity_id',
array()
);

how to update Cart Options in Codeigniter?

Through Documents provided by Codeigniter Its Cart Library doesnt Update its options We can add the Options like that
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
)
);
$this->cart->insert($data);
Is there any other way or any tutorial to learn how we can update options of Cart just like
$qid = $this->input->post("qid");
$pairs = $this->input->post("pairs");
$males = $this->input->post("males");
$females = $this->input->post("females");
$data = array(
array(
'rowid' => $qid,
'qty' => 1,
'options' => array('pairs' => $pairs, 'males' => $males, 'females' => $females))
);
$this->cart->update($data);
I have searched it but seems no one has made any fix for it?
Though I have not found any solution therefore I am using simple solution and that is just rest the item in cart and adding new item with same id and updated option values. Its not great trick though but it is just working for me.
I am fallen yesterday like this kind of problem then i create Extending / Override update function. Here how to Extending Native library Link : Create CI Library
Here is my modified code in Github Link
I hope it will help someone and also can modify as their need.
No it did not work for me.. but there is no any errors, I check the array within the _update function as well,
Array
(
[id] => 177
[rowid] => 66bd8895e10f189f62bf3a65ada83630
[qty] => 1
[options] => Array
(
[vat] => 0
[discount] => 0
)
)
I know i'ts just too late but I want to share my solution:
I just got the the options in an array and updated it as I wanted and then update the entire options field in the cart with the array content.
Hope this help somebody.
Sorry for the english.
The way I tried was a bit difficult but it works. You can’t just update a single option value. To update you have to pass all the existing values to all the option values with the value you want to update as well.
If you want to update your Size option
$data = array(
‘rowid’ => $yourRowIdHere,
‘options’ => array ( ‘color’ => $yourExistingValue, ‘length’ => $yourExistingValue, ‘size’ => $newUpdatedValue)
));
$this->cart->update($data);
Give it a try :)

Names of the associated products of a configurable product are not visible in admin?

In a vanilla install of Magento 1.5.1.0 I have created simple products COLOR-RED, COLOR-BLUE and created a configurable product COLOR that has these products associated with it. This works all fine, except for the fact that the names of the associated products are not shown in the backend in the Configurable product's 'Super product attributes configuration' table.
I have added a simple debug statement to htdocs/app/design/adminhtml/default/default/template/widget/grid.phtml to display the item's data before display. See below, the name attribute is not there.
Does anybody have an idea why? Is it a bug in Magento, or is something else wrong?
Array
(
[entity_id] => 1
[entity_type_id] => 4
[attribute_set_id] => 9
[type_id] => simple
[sku] => COLOR-RED
[has_options] => 0
[required_options] => 0
[created_at] => 2011-12-13 15:08:36
[updated_at] => 2011-12-13 15:08:36
[is_saleable] => 0
[inventory_in_stock] => 0
[color] => 3
[price] => 12.0000
[stock_item] => Varien_Object Object
(
[_data:protected] => Array
(
[is_in_stock] =>
)
[_hasDataChanges:protected] =>
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] =>
)
)
You're probably using the Simple Configurable Products module. In the file app/code/community/OrganicInternet/SimpleConfigurableProducts/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Grid.php the developers of Organic Internet left a nice little hint;
#Copied from Magento v1.3.1 code.
#Only need to comment out addFilterByRequiredOptions but there's no
#nice way of doing that without cutting and pasting the method into my own
#derived class. Boo.
#This change stops the filtering-out of any configurable product's 'associated products' that have compulsory custom options
#Have also replaced parent::_prepareCollection with Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
I figured the code that was used in 1.3.1 was outdated so this file would be outdated if you're using >1.3.1. So I just looked at the contents of the core file app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Super/Config/Grid.php and looked for differences, and found the culprit.
All you have to do is add this line;
->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner')
between these two lines;
->addFieldToFilter('attribute_set_id',$product->getAttributeSetId())
->addFieldToFilter('type_id', $allowProductTypes);
And you're all set!

Magento: Combine eq and is null in addAttributeToFilter

I want to combine with OR "is null" and "eq" => array()
$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115,116));
But this does not work.
To use addAttributeToFilter() with an OR condition you can pass an array of arrays like this:
$collection->addAttributeToFilter(
array(
array(
'attribute' => 'name_of_attribute_1',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'name_of_attribute_2',
'in' => array(115, 116)
)
)
);
The definition of filtering against NULL keywords may look somewhat confusing at first glance, but it's plain simple, once you get used to it.
Magento supports two filter types of type string, namely 'null' and 'notnull', for comparisions against NULL keywords.
Be aware, that even though you need to pass key=>value pairs (because an associative array is used), the value will be always ignored, when filter type 'null' or 'notnull' is used.
Example
var_dump(
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
array(
array(
'attribute' => 'description',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'sku',
'in' => array(115, 116)
)
)
)
->getSelectSql(true)
);
The output of the dump may vary depending on your shop configuration (attribute ids, count of stores, etc.), but the OR logic in the WHERE clause should always remain the same:
SELECT
`e`.*,
IFNULL(_table_description.value, _table_description_default.value) AS `description`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_entity_text` AS `_table_description_default` ON
(_table_description_default.entity_id = e.entity_id) AND
(_table_description_default.attribute_id='57') AND
_table_description_default.store_id=0
LEFT JOIN
`catalog_product_entity_text` AS `_table_description` ON
(_table_description.entity_id = e.entity_id) AND
(_table_description.attribute_id='57') AND
(_table_description.store_id='1')
WHERE (
(IFNULL(_table_description.value, _table_description_default.value) is NULL) OR
(e.sku in (115, 116))
)
I have product attribute is_expired with Yes and No value and i want only those product have NO value.below query work for me that get record that have neq 1 and not null.
Combine not equal to and null condition WITH OR.
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
You can changed neq with eq.
To use addAttributeToFilter with an OR condition and with left join
you can do something like that:
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);

Multiple level ordering in Yii

This is my CActiveDataProvider
$dataProvider = new CActiveDataProvider ('MyTable', array (
'pagination' => array (
'PageSize' => 4,
),
'criteria' => array (
'condition' => 'from_user_id='.$user->id,
'order' => 'date DESC',
),
));
My question is: I would like to have this CActiveDataProvider ordered first by date (as the posted code does), and in a second level, within those with the same date, order by a second criteria. Is this possible?
Maybe I'm not understanding you correctly, but won't the following work? :
'order' => 'date DESC, name DESC',

Resources