1

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!

1
  • After studying a little bit more addAttributeToSelect I really feel like $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.
    – baku
    Sep 3 '15 at 11:41
1

unrelated issue was causing interference. Working code below.

 if ($attribute == 'rebate' && $storeId != 0) {
        $this->addPriceData();
        $this->addAttributeToFilter(
            array(
                array(
                    'attribute' => 'special_price_type',
                    'value'     => array('1340', '1339')
                )
            )
        );
        $this->addAttributeToSelect(array(
            'msrp',
            'price',
            'special_price'
        ));
        $this->addExpressionAttributeToSelect(
            'ratio',
            '(ROUND( ( 1 - ( {{price}} - {{special_price}} ) )*100 ))',
            array(
                'special_price' => 'special_price',
                'price'         => 'price',
            )
        ); //
        $this->getSelect()->order("ratio {$dir}");
        return $this;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.