2

I need to read the condition of a shopping cart price rule in magento programatically.

Mage_SalesRule_Model_Rule has a method getConditionsSerialized() which does provide the conditions but in a cryptic manner to say the least such as the following:

a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_address";s:9:"attribute";s:13:"base_subtotal";s:8:"operator";s:2:">=";s:5:"value";s:1:"1";s:18:"is_value_processed";b:0;}}}

Does anyone know how to read the condition in a more humane way plz? I'd like to be able to read certain attributes.

Thanks a bunch!

Krt_Malta

Krt_Malta
  • 9,265
  • 18
  • 53
  • 91

3 Answers3

5

You can use php's unserialize function to convert this to a workable array.

Try this:

$conditions = unserialize($rule->getConditionsSerialized());
print_r($conditions);

I've blogged before about how these work, and while my post is about creating rules, it still explains a bit about how you might come to understand conditions.

Here's my post

Hope it helps!

Magento Guy
  • 2,493
  • 1
  • 16
  • 13
1

unserialize($rule->getConditionsSerialized()) did the trick!

Hope it helps someone else!

Krt_Malta
  • 9,265
  • 18
  • 53
  • 91
1

Magento already implements a function to unserialize conditions and actions for itself, all you have to do is use it.

$conditions = Mage::getModel('salesrule/rules')->load($ruleId)->getConditions();

Each call to ->getConditions() will get you deeper conditions within executing rule object.

For example, first time you call ->getConditions() it will return an object type of Mage_SalesRule_Model_Rule_Condition_Combine that's the default wrapper for all inner conditions. If you chain the call again ->getConditions()->getConditions() you'll get an array with [X] of condition objects, each one of the type you declared when creating them through adminhtml interface.

Avoid using unserialize() and start using Magento built-in functions.

dchayka
  • 1,291
  • 12
  • 20