I've been trying to apply this post:
How to add dynamic field in magento collection?
to my own simple scenario.
I have implemented a rewrite of Mage_Catalog_Model_Resource_Product_Collection to handle a custom sort option in catalog view.
I need to pull two attributes (msrp and special_price) do some math with them to create a 'virtual' field upon which to sort.
class NAMESPACE_MODULE_Model_Resource_Product_Collection
extends Mage_Catalog_Model_Resource_Product_Collection
{
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
if ($attribute == 'rebate' && $storeId != 0) {
$this->addPriceData();
$this->addAttributeToFilter(
array(
array(
'attribute' => 'special_price_type',
'value' => array('1340', '1339')
)
)
);
$this->addAttributeToSelect(
array('special_price', 'msrp')
);
$this->addExpressionAttributeToSelect(
'ratio',
"[pseudocode: ( 1 - (mrsp - special_price))*100 ]"
); //select
$this->getSelect()->order("ratio {$dir}");
return $this;
}
Struggling to understand how to manipulate those values in addExpressionAttributeToSelect. Nor am I clear whether those values are even exposed at this point. I would assume so because of:
$this->addAttributeToSelect(
array('special_price', 'msrp')
);
However dropping a breakpoint there and stepping through the code, I can't find them anywhere in the instantiated object.
Any guidance welcome!
$this->addExpressionAttributeToSelect( 'ratio', '( ( 1 - ( {{msrp}} - {{special_price}}))*100 )', array( 'special_price'=>'special_price', 'msrp'=>'msrp', ) ); //
Should be giving me the result i need. I'm no longer getting SQL errors this way. But sorting does appear to be default.