diff --git a/modules/commerce_coupon/commerce_coupon.rules.inc b/modules/commerce_coupon/commerce_coupon.rules.inc index 201c79a..8c8ee10 100644 --- a/modules/commerce_coupon/commerce_coupon.rules.inc +++ b/modules/commerce_coupon/commerce_coupon.rules.inc @@ -50,6 +50,36 @@ function commerce_coupon_rules_event_info() { } /** + * Implements hook_rules_condition_info(). + */ +function commerce_coupon_rules_condition_info() { + $conditions = array(); + + $conditions['commerce_coupon_compare_coupon'] = array( + 'label' => t('Coupon line item exists'), + 'parameter' => array( + 'commerce_order' => array( + 'type' => 'commerce_order', + 'label' => t('Order'), + 'description' => t('The order whose line items should be checked for an existing coupon line item.'), + ), + 'coupon' => array( + 'type' => 'text', + 'label' => t('Coupon'), + 'options list' => 'commerce_coupon_coupons_options_list', + 'description' => t('Limit the search amongst the coupon line items to a particular coupon.'), + ), + ), + 'group' => t('Commerce Coupon'), + 'callbacks' => array( + 'execute' => 'commerce_coupon_rules_line_item_exists', + ), + ); + + return $conditions; +} + +/** * Implements hook_rules_action_info(). */ function commerce_coupon_rules_action_info() { @@ -159,6 +189,59 @@ function commerce_coupon_rules_action_info() { return $actions; } +/** + * Checks an order for the existence of a coupon line item. + * + * @param $order + * The order to check for a coupon line item. + * @param $coupon + * The machine-name of a particular coupon to search for; if '-any-' + * the condition returns TRUE for any found coupon line item. + */ +function commerce_coupon_rules_line_item_exists($order, $coupon) { + $order_wrapper = entity_metadata_wrapper('commerce_order', $order); + + // Loop over all the line items on the order. + foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) { + // If the current line item is a coupon line item and the condition either + // doesn't care about the coupon or the coupon name matches, return TRUE. + if ($line_item_wrapper->type->value() == 'coupon' && + ($coupon == '-any-' || $line_item_wrapper->commerce_coupon_reference->value() == $coupon)) { + return TRUE; + } + } + + return FALSE; +} + +/** + * Returns a list of coupons available for existence conditions. + */ +function commerce_coupon_coupons_options_list() { + // Return an options array with an "Any" option prefixed to the list of existing coupon options. + return array_merge(array('-any-' => t('Any')), commerce_coupon_options_list()); +} + +/** + * Return list of defined coupons as an array of all coupon codes keyed by the coupon id. + * @todo: complete this function. + */ +function commerce_coupon_options_list() { + $result = db_query("SELECT coupon_id FROM {commerce_coupon}"); + + $coupon_ids = array(); + foreach ($result as $coupon) { + $coupon_ids[] = $coupon->coupon_id; + } + + $coupons = commerce_coupon_load_multiple($coupon_ids); + $coupon_options = array(); + foreach ($coupons as $coupon) { + $coupon_options[$coupon->coupon_id] = $coupon->commerce_coupon_code[LANGUAGE_NONE][0]['safe_value']; + } + + return $coupon_options; +} function commerce_coupon_action_is_valid_coupon($override = FALSE) { $validation_results = &drupal_static('commerce_coupon_action_validation_results');