1

We have a Magento CE 1.9x shop that we developed a message that shows on the product page with a coupon, but we need to ONLY show the message IF the coupon (price rule) IS VALID for that item. Is there a reasonably simple way to test if the coupon is valid for that item WITHOUT actually applying it?

1

Since the conditions tie a coupon (salesrule) to a product you could check the conditions_serialized field in the salesrule table.

The conditions are serialized before storing it in there but with some fancy mapping I'm sure you can check if a certain SKU is in the conditions.

Still, I'm guessing you only have a limited amount of coupons. A simpler and cleaner solution would probably be to make an attribute where you indicate per product what coupon applies. But that might also depend on the number of products you have

  • Yeah, we have like 3500 products that is increasing quickly, and the coupons change regularly. Our marketing team handles the coupons (allowing on certain products and not on others) and I don't want them messing with the actual products to do so, so I'd like to have a way to check if the coupon can be applied. The other catch is that in this case it's ALWAYS the same coupon code in this case. You would think that it would be easy enough to say "Can code XYZ be applied to this product? If yes, show this message." – C.Miller Sep 1 '15 at 15:09
  • Hm in that case i'd probably go with a query in the conditions_serialized column. That shouldn't be too hard – Sander Mangel Sep 1 '15 at 15:11
1

i spent all day on this and put together from a few things i found online. this works on the magento product page. it checks a specific rule and goes into the actions of that rule to see if the item is there. this code is only if there is one specific sku set in the rule. but there is enough code here to figure out to do other things.

$ruleid = 54; //get the rule id from the admin
$oRule = Mage::getModel('salesrule/rule')->load($ruleid, 'rule_id');
$actions = unserialize($oRule->getActionsSerialized());
foreach ($actions as $key=>$val){
    if (is_array($val)){
        foreach($val as $keyb=>$valb){
            $switch= $valb['value'] == $MySKU ? 1 : 0;
        }       
    }
}   

the above code for $switch is based off of sku. and that is if there is if there is only one SKU in the action. if you want to check the action before the switch you can use

$valb['attribute'] == 'sku'

also, if you have more then one SKU in the action then you would need explode the $valb['value'] and add another loop.

0

I don't think you can have a silver bullet solution since the rules for a coupon don't involve only product attributes. They can involve quote settings. Also a rule may be valid if 2 or more products are in the cart at the same time.
And the possible combinations are infinite (almost), so you cannot test all of them

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.