I have not tried this, but I believe it's possible. I don't have the exact code, but I can provide you with it later when I have more free time if you really need it. This appraoch is pretty straightforward.
There's a table called catalogrule_product
that stores Catalog Price Rules
discounts per product and customer group. Note that this table needs to get indexed every time you make changes to your Catalog Price Rules
. You can do this with the indexer or by clicking "Apply Rules" on this page (this could take long if you have a lot of products).
For a given product you have on a catalog (or any) page, get its entity_id
and look it up on catalogrule_product.product_id
. Make sure to check customer_group_id
as well. Once you find the row(s) you're looking for, this row(s) will have have timestamps (in seconds) from_time
and to_time
for that given applied rule (rule_id
).
Get its row_id
and look this up on catalogrule
if you need more information on the rule. The entry you find on catalogrule
has from_date
and to_date
, and those are all you need to calculate what you want.
I was faced with this some time ago. Here is what I came up with.
I've created a module in which I've overwritten the addAttributeToSort()
method in the product collection.
Let's call the module Easylife_Sorting
.
app/etc/modules/Easylife_Sorting.xml
- the declaration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Sorting>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Catalog />
</depends>
</Easylife_Sorting>
</modules>
</config>
app/code/local/Easylife/Sorting/etc/config.xml
- the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Sorting>
<version>1.0.0</version>
</Easylife_Sorting>
</modules>
<global>
<models>
<catalog_resource>
<rewrite>
<product_collection>Easylife_Sorting_Model_Resource_Product_Collection</product_collection>
</rewrite>
</catalog_resource>
</models>
</global>
</config>
app/code/local/Easylife/Sorting/Model/Resource/Product/Collection.php
- overwritten collection - follow the comments in the code
<?php
class Easylife_Sorting_Model_Resource_Product_Collection
extends Mage_Catalog_Model_Resource_Product_Collection {
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC){
//don't screw up the admin sorting
if (Mage::app()->getStore()->getId() == Mage_Core_Model_App::ADMIN_STORE_ID){
return parent::addAttributeToSort($attribute, $dir);
}
//use sorting by image or not only in the default sorting.
//if the customer sorts by name, let them sort by name.
//showing products with images first will confuse them if they sorted by name or price or something else other than position
if ($attribute == 'position') {
$useAttribute = 'small_image'; //image attribute to use: can also be 'image' or 'thumbnail'
$this->addAttributeToSelect($useAttribute);
//add a 1|0 flag for products depending on:
// - the image attribute value is empty
// - the image attribute value is null
// - the image attribute value is set to 'no_selection'
$this->addExpressionAttributeToSelect(
'has_image',
"(CASE WHEN {{image}} = '' OR {{image}} IS NULL OR {{image}} = 'no_selection' THEN 0 ELSE 1 END)",
array('image'=>$useAttribute)
);
//sort by the flag set above and then by position
$this->getSelect()->order(array('has_image DESC', 'position '.$dir));
return $this;
}
//if not sorting by position let magento do its magic
else {
return parent::addAttributeToSort($attribute, $dir);
}
}
}
Of course this can be improved by adding a config setting to enable or disable this functionality but this should be easy.
Best Answer
If you don't have access to the
$collection
you can use a jQuery to parse all ids:Result:
But better way is to take all ids right from the product collection:
Magento does not use any ui-components in the standard product listing block, so they cannot be detected using the registry.
Update
You can add own block to the layout:
Block code:
Template where you can use the product collection to fill your javascript object (you can get a sku, name and other information right from the collection):
Results (tested with standard layered navigation):
11 products:
With filter (1 product):