/** * Unit test for eZDiscountRule::fetchByUserIDArray() */ public function testFetchByUserIDArray() { // Create 5 few discount rules $discountRules = array(); for ($i = 0; $i < 5; $i++) { $row = array('name' => __FUNCTION__ . " #{$i}"); $rule = new eZDiscountRule($row); $rule->store(); $discountRules[] = $rule; } // Create 5 user discount rules for 3 different user IDs $usersDiscountRules = array(); foreach (array(1, 3) as $userID) { $usersDiscountRules[$userID] = array(); $userDiscountRules =& $usersDiscountRules[$userID]; foreach ($discountRules as $discountRule) { $row = array('discountrule_id' => $discountRule->attribute('id'), 'contentobject_id' => $userID); $userDiscountRule = new eZUserDiscountRule($row); $userDiscountRule->store(); $userDiscountRules[] = $userDiscountRule; } } // fetch the discount rules for user #1 and #2. This will match 10 // eZUserDiscountRule, and return the 5 rules, since no duplicates will // be returned $rules = eZUserDiscountRule::fetchByUserIDArray(array(1, 2)); $this->assertInternalType('array', $rules, "Return value should have been an array"); $this->assertEquals(5, count($rules), "Return value should contain 5 items"); foreach ($rules as $rule) { $this->assertInstanceOf('eZDiscountRule', $rule); } }
function discountPercent() { $discountPercent = 0; $user = eZUser::currentUser(); $userID = $user->attribute('contentobject_id'); $nodes = eZContentObjectTreeNode::fetchByContentObjectID($userID); $idArray = array(); $idArray[] = $userID; foreach ($nodes as $node) { $parentNodeID = $node->attribute('parent_node_id'); $idArray[] = $parentNodeID; } $rules = eZUserDiscountRule::fetchByUserIDArray($idArray); foreach ($rules as $rule) { $percent = $rule->attribute('discount_percent'); if ($discountPercent < $percent) { $discountPercent = $percent; } } return $discountPercent; }
static function discountPercent($user, $params) { $bestMatch = 0.0; if (is_object($user)) { $groups = $user->groups(); $idArray = array_merge($groups, array($user->attribute('contentobject_id'))); // Fetch discount rules for the current user $rules = eZUserDiscountRule::fetchByUserIDArray($idArray); if (count($rules) > 0) { $db = eZDB::instance(); $i = 1; $subRuleStr = ''; foreach ($rules as $rule) { $subRuleStr .= $rule->attribute('id'); if ($i < count($rules)) { $subRuleStr .= ', '; } $i++; } // Fetch the discount sub rules $subRules = $db->arrayQuery("SELECT * FROM\n ezdiscountsubrule\n WHERE discountrule_id IN ( {$subRuleStr} )\n ORDER BY discount_percent DESC"); // Find the best matching discount rule foreach ($subRules as $subRule) { if ($subRule['discount_percent'] > $bestMatch) { // Rule has better discount, see if it matches if ($subRule['limitation'] == '*') { $bestMatch = $subRule['discount_percent']; } else { // Do limitation check $limitationArray = $db->arrayQuery("SELECT * FROM\n ezdiscountsubrule_value\n WHERE discountsubrule_id='" . $subRule['id'] . "'"); $hasSectionLimitation = false; $hasClassLimitation = false; $hasObjectLimitation = false; $objectMatch = false; $sectionMatch = false; $classMatch = false; foreach ($limitationArray as $limitation) { if ($limitation['issection'] == '1') { $hasSectionLimitation = true; if (isset($params['section_id']) && $params['section_id'] == $limitation['value']) { $sectionMatch = true; } } elseif ($limitation['issection'] == '2') { $hasObjectLimitation = true; if (isset($params['contentobject_id']) && $params['contentobject_id'] == $limitation['value']) { $objectMatch = true; } } else { $hasClassLimitation = true; if (isset($params['contentclass_id']) && $params['contentclass_id'] == $limitation['value']) { $classMatch = true; } } } $match = true; if ($hasClassLimitation == true and $classMatch == false) { $match = false; } if ($hasSectionLimitation == true and $sectionMatch == false) { $match = false; } if ($hasObjectLimitation == true and $objectMatch == false) { $match = false; } if ($match == true) { $bestMatch = $subRule['discount_percent']; } } } } } } return $bestMatch; }