I would like to use OR in magento mysql query, I am looking for an equivalent in magento
where (is_in_stock=0 and show_in_front=1) and (is_in_stock=1 and show_in_front=1)
I know following can be used for 'OR', but am not sure how to produce an equiavlent query as above;
->addAttributeToFilter(
array(
'attribute' => 'is_in_stock',
'eq' => '0',
),
Following is example code for OR condition
$collection->addAttributeToFilter(
array(
array('attribute'=> 'attribute1','like' => 'value1'),
array('attribute'=> 'attribute2','like' => 'value2'),
array('attribute'=> 'attribute3','like' => 'value3'),
)
);
Please check this query
//OR Condition
->addAttributeToFilter(array(array('attribute' => 'is_in_stock','eq' => '0'),array('attribute' => 'show_in_front','eq' => '1')));
//And Condition
->addAttributeToFilter('is_in_stock', array('eq' => '1'));
->addAttributeToFilter('show_in_front', array('eq' => '1'));
Related
We have been trying to make a query when a user apply some filters on front end. We are trying to make OR query in products collection: tried the following but none of them work
$collection->addAttributeToFilter(['web_mobile_filter','offer_group_name_value'],
[
['in' => '1122'],
['in' => '72']
]);
and
$collection->addAttributeToFilter(
[
[
'attribute' => 'web_mobile_filter',
'in' => '1122'
],
[
'attribute' => 'offer_group_name_value',
'in' => '72,73'
],
]
);
Can someone help me, find me what i am doing wrong here please?
You can write like this :-
$collection->addAttributeToFilter( 'web_mobile_filter',
[
'in' => '1122'
]);
$collection->addAttributeToFilter(array( array('attribute' => 'special_price','null' => true), array('attribute' => 'product_in_stock','eq' => 1)), '', 'left' );
I have some Magento code that I'm trying to use to filter a collection of products. I want to find all products where the date is BEFORE a certain date OR the date hasn't been set (ie is null).
I have:
function getProduct($product_id) {
global $proxy, $sessionId, $conn, $start_date, $time_to_run;
if ($product_id == 'all') {
$result=Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('price_adjust_active', array('null' => true))
->addAttributeToFilter('price_adjust_active', '1')
->addAttributeToFilter(array(
array('attribute'=> 'price_adjust_last_run','lt' => date('Y-m-d H:i:s', $time_to_run)),
array('attribute'=> 'price_adjust_last_run', 'null' => true)
))
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setOrder('entity_id', 'DESC')
->setPageSize(1);
} else {
$result=Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('eq' => $product_id))
->addAttributeToFilter('price_adjust_active', array('null' => true))
->addAttributeToFilter('price_adjust_active', '1')
->addAttributeToFilter(array(
array('attribute'=> 'price_adjust_last_run','lt' => date('Y-m-d H:i:s', $time_to_run)),
array('attribute'=> 'price_adjust_last_run', 'null' => true)
))
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setOrder('entity_id', 'DESC')
->setPageSize(1);
}
and I can successfully filter out the products with the dates set before my specified date. I just can't get the "null" attribute to work. As you can see from my code, I have 2 different filters in there, and neither of them seem to give me the desired results.
The two faulty attempts are:
->addAttributeToFilter('price_adjust_active', array('null' => true))
or
array('attribute'=> 'price_adjust_last_run', 'null' => true)
Magento has a special way to filter dates instead of just using a straight greater or less than.
Try this...
->addAttributeToFilter('price_adjust_last_run', array('or'=> array(
0 => array(
'date' => true,
'from' => date('Y-m-d H:i:s', $time_to_run - 1)
),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
You are setting date to true and then you'll probably want to make sure to subtract 1 second from your $time_to_run variable.
This works similarly with addFieldToFilter() as well.
I want to select products collection with condition "A or (B and C)" by using AddAttributeToFilter, but I have no idea..
Can someone help me?
$collection = Mage::getModel('xyz/abc')->getCollection();
$collection->addAttributeToFilter(
array(
array('attribute'=> 'someattribute','like' => 'value'),
array('attribute'=> 'otherattribute','like' => 'value'),
array('attribute'=> 'anotherattribute','like' => 'value'),
)
);
$collection->addAttributeToFilter('status', array('eq' => 1));
Transnational will be look like
WHERE ((someattribute LIKE 'value') OR (otherattribute LIKE 'value') OR (anotherattribute LIKE 'value')) and status=1
You can also visit this link for more info
Magento addFieldToFilter: Two fields, match as OR, not AND
In CakePHP, I would like to sort my index list (created by the paginator component) on sequence ASC, but it won't work. If I see the query setup in the CakePHP docs (http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#query-setup), my paginator settings should look like this:
public function index()
{
$this->Paginator->settings = array(
'Attraction' => array(
'conditions' => array(
'Attraction.deleted' => null
),
'order' => array(
'Attraction.sequence ASC',
'Attraction.id ASC'
),
'limit' => 15
)
);
$attractions = $this->Paginator->paginate('Attraction');
$this->set('attractions', $attractions);
}
But every time I load my index file, the list is sorted on ID and ignores the "order" setting. Can anybody tell me if there's anything wrong with my "order" item in my paginator settings? ;)
Thx!
try
'order' => array(
'Attraction.sequence' => 'asc',
'Attraction.id' => 'asc'
),
edit:
The sorting direction must be in the array value, while the field name should be the index. The examles I found in the documentations are always showing that
I don't think you should have that Attraction index in your options array - in other words, I think your options array should look like this:
$this->Paginator->settings = array(
'conditions' => array(
'Attraction.deleted' => null
),
'order' => array(
'Attraction.sequence ASC',
'Attraction.id ASC'
),
'limit' => 15
);
The following code does create a configurable product, however, when I open the product in the backend, the following message appears:
Select Configurable Attributes
"Only attributes with scope "Global", input type "Dropdown" and Use To Create Configurable Product "Yes" are available."
A single checkbox is displayed ("Colour Group"), which must be selected before continuing.
When I click "Continue", all of the product data is there as expected EXCEPT for the associated products.
//Mage Product
$mpr = Mage::getModel('catalog/product');
$mpr
->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
->setTaxClassId(5)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setWebsiteIds(array(1))
->setAttributeSetId(4) // You can determine this another way if you need to.
->setSku("C12345")
->setName("C12345")
->setQty(25)
->setShortDescription('short description')
->setDescription('description')
->setPrice(1)
->setStockData(array(
'use_config_manage_stock' => 1,
'is_in_stock' => 1,
'is_salable' => 1,
));
$productData = array(
'7039604' =>
array('0' => array('attribute_id' => '85', 'label' => 'ROYAL','value_index' => '28563', 'is_percent' => 0, 'pricing_value' => '')
,'1' => array('attribute_id' => '192', 'label' => '14', 'value_index' => '28728', 'is_percent' => 0, 'pricing_value' => '')
)
);
$attributeData = array(
'0' => array(
'id' => NULL
,'label' => 'Color'
,'position' => NULL
,'values' => array(
'0' => array('value_index' => 28563, 'label' => 'ROYAL', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '85')
)
,'attribute_id' => 85
,'attribute_code' => 'color'
,'frontend_label' => 'Color'
,'html_id' => 'config_super_product__attribute_0')
,'1' => array(
'id' => NULL
,'label' => 'Rivers Size'
,'position' => NULL
,'values' => array(
'0' => array('value_index' => 28728, 'label' => '14', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '192')
)
,'attribute_id' => 192
,'attribute_code' => 'rivers_size'
,'frontend_label' => 'Rivers Size'
,'html_id' => 'config_super_product__attribute_1')
);
$mpr->setConfigurableProductsData($productData);
$mpr->setConfigurableAttributesData($attributeData);
$mpr->setCanSaveConfigurableAttributes(true);
$mpr->save();
Add this code before $mpr->save();
$SKU = "any-simple product sku enter here";
$productid = Mage::getModel('catalog/product')
->getIdBySku(trim($SKU));
$mpr->assignProduct($productid);
And set simple product sku in $SKU variable. and i have check that when i select global variable then after i see associated product in configure product.
Its work fine !!!
If you are getting redirected to select attribute page, this means that attribute data you set in this sample is not saved correctly.
Try viewing catalog_product_super_attribute after script run (new rows should be added).