it took me a while to figure this out. Should work for you, if you customy it for your needs.
To list products affected by a price rule, you must make your way over the rule-model.
Here is the code:
<?php
// we need the date to filter for the special price
$dateToday = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
$dateTomorrow = date('m/d/y', $tomorrow);
// this gets the product collection and filters for special price;
// grabs products with special_from_date at least today, and special_to_date at least tomorrow
$_productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $dateToday))
->addAttributeToFilter('special_to_date', array('date' => true, 'from' => $dateTomorrow))
->load();
// this loads the standard magento catalog proce rule model
$rules = Mage::getResourceModel('catalogrule/rule_collection')->load();
// read: if there are active rules
if($rules->getData()) {
$rule_ids = array(); // i used this down below to style the products according to which rule affected
$productIds[] = array(); // this will hold the ids of the products
foreach($rules as $rule) {
$rule_ids[] = $rule->getId();
$productIds = $rule->getMatchingProductIds(); // affected products come in here
}
// merge the collections: $arr is an array and keeps all product IDs we fetched before with the special-price-stuff
$arr = $_productCollection->getAllIds();
if($productIds) {
// if there are products affected by catalog price rules, $arr now also keeps their IDs
$arr = array_merge($arr,$productIds);
}
// we initialize a new collection and filter solely by the product IDs we got before, read: select all products with their entity_id in $arr
$_productCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id',array('in'=>$arr))
->load();
}
== UPDATE ==
I added some comments to the code and renamed $skus
to $productIds
(which fits better).
If you are still getting errors, please let me know what's exactly happening. This code is tested for Magento v1.7.0.2.
Cheers
Simon
You could do it using the item filter of the rule (Apply the rule only to cart items matching the following conditions ). Unfortunately, the list of operators is limited and adding aggregate functions (MAX, MIN) would be a non-trivial task.
Anyway, what you're after is just one of a few neat features of this extension: http://www.magentocommerce.com/magento-connect/special-promotions-pro-5784.html
Best Answer
There isn't an easy way to create an "On Sale" button in your layered navigation since 3 attributes make up the "On Sale":
Special Price
,Date From
,Date To
.To have one link or option for "On Sale" you would have to add a new attribute to products named "On Sale" or something along that line.
It would have to be a dropdown, with for example the options "Yes" and "No" which would enable you to also turn on
Use In Layered Navigation
for this attribute.Now you can set it to "Yes" for all products that have a special price. This does require you to actually set the attribute so no nice automation going in and out of sale on the from and to date but it would give you the desired effect on the frontend.