0

I want to make an if else in filterArgs (in Model) as follow:

public $filterArgs = array(
            array('name' => 'to', 'type' => 'value' => 'Product.regular_price' ),
        );

I want the field to change base on the condition.

if sale = 1, field is promo_price, else field is regular_price

I already try below code (but unsuccessful):

'field'=> 'Product.sale' => 1 ? 'Product.promo_price >=' : 'Product.regular_price >='

Can someone please help me. Thanks alot in advance!

1 Answer 1

1

I would create a virtualField in my Product Model

$virtualFields = array 
(
    'my_price' => 'IF(Product.sale = 1, Product.promo_price, Product.regular_price)';
)

public $filterArgs = array
(
    'my_price' => array('type' => 'value'),
);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, can you explain 'from' => 'IF(Product.sale = 1, Product.promo_price, Product.regular_price)', so far, no error. but the result somehow wrong.
Instead of creating the IF in php I created it in mysql using virtualFields. I created a new column named my_price wich value is promo_price rather than regular_price based on sale column vale. Then I filter results by my_price value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.