diff --git a/commerce_coupon.module b/commerce_coupon.module index 1c526f4..d0ff2a3 100644 --- a/commerce_coupon.module +++ b/commerce_coupon.module @@ -425,6 +425,59 @@ function commerce_coupon_type_configure($bundle, $reset) { } } + // Look for or add the number of uses. + $field_name = 'commerce_coupon_number_of_uses'; + $field = field_info_field($field_name); + $instance = field_info_instance($entity_type, $field_name, $bundle); + + if (empty($field) || $reset) { + $field_data = array( + 'field_name' => $field_name, + 'type' => 'number_integer', + 'label' => t('Maximum Number of Uses'), + 'cardinality' => 1, + 'entity_types' => array($entity_type), + 'translatable' => FALSE, + 'locked' => FALSE, + 'settings' => array(), + ); + + if (empty($field)) { + $field = field_create_field($field_data); + } + else { + $field = field_update_field($field_data); + } + } + + if (empty($instance) || $reset) { + $instance_data = array( + 'field_name' => $field_name, + 'entity_type' => $entity_type, + 'bundle' => $bundle, + + 'label' => t('Number of Uses'), + 'required' => FALSE, + 'display' => array(), + 'settings' => array( + 'min' => '0', + ), + 'default_value' => array( + 0 => array( + 'value' => 1 + ) + ), + ); + + + if (empty($instance)) { + field_create_instance($instance_data); + } + else { + field_update_instance($instance_data); + } + } + // Allow other modules to configure coupon types: module_invoke_all('commerce_coupon_type_configure', $bundle, $reset); } diff --git a/commerce_coupon.rules.inc b/commerce_coupon.rules.inc index e237109..4817efa 100644 --- a/commerce_coupon.rules.inc +++ b/commerce_coupon.rules.inc @@ -121,6 +121,24 @@ function commerce_coupon_rules_action_info() { 'base' => 'commerce_coupon_action_get_coupons_for_order', ); + $actions['commerce_coupon_action_get_coupons_for_order'] = array( + 'label' => t('Get coupons for order'), + 'parameter' => array( + 'commerce_order' => array( + 'type' => 'commerce_order', + 'label' => t('Commerce order'), + ), + ), + 'provides' => array( + 'order_coupons' => array( + 'type' => 'list', + 'label' => 'Coupons attached to this order', + ), + ), + 'group' => t('Commerce Coupon'), + 'base' => 'commerce_coupon_action_get_coupons_for_order', + ); + $actions['commerce_coupon_action_create_coupon_line_item'] = array( 'label' => t('Create coupon line item'), 'parameter' => array( @@ -202,16 +220,20 @@ function commerce_coupon_action_set_granted_amount($commerce_coupon_log, $amount $coupon = $commerce_coupon_log->coupon; + // Get the price component to use in this price. + $price_component_name = 'commerce_coupon_' . $coupon->type; + drupal_alter('commerce_coupon_price_component_name', $price_component_name, $coupon); + // Set the unit price on the line item object. $commerce_coupon_log->commerce_granted_amount->amount = $amount; $commerce_coupon_log->commerce_granted_amount->currency_code = $currency_code; // Add the base price to the components array. - if (!commerce_price_component_load($commerce_coupon_log->commerce_granted_amount->value(), 'commerce_coupon_' . $coupon->type)) { + if (!commerce_price_component_load($commerce_coupon_log->commerce_granted_amount->value(), $price_component_name)) { $commerce_coupon_log->commerce_granted_amount->data = commerce_price_component_add( $commerce_coupon_log->commerce_granted_amount->value(), - 'commerce_coupon_' . $coupon->type, + $price_component_name, $commerce_coupon_log->commerce_granted_amount->value(), TRUE, FALSE @@ -230,12 +252,21 @@ function commerce_coupon_action_get_coupons_for_order($commerce_coupon) { return array('order_coupons' => commerce_coupon_get_coupons_by_order($commerce_coupon->order_number)); } -function commerce_coupon_action_create_coupon_line_item($commerce_coupon, $commerce_order, $amount, $currency_code) { +function commerce_coupon_action_get_coupons_for_order($commerce_coupon) { + if (!$commerce_coupon) { + return array(); + } + return array('order_coupons' => commerce_coupon_get_coupons_by_order($commerce_coupon->order_number)); +} +function commerce_coupon_action_create_coupon_line_item($commerce_coupon, $commerce_order, $amount, $currency_code) { if (!($commerce_order instanceof EntityMetadataWrapper)) { $commerce_order = entity_metadata_wrapper('commerce_order', $commerce_order); } + // Get the price component to use in this price. + $price_component_name = 'commerce_coupon_' . $commerce_coupon->type; + drupal_alter('commerce_coupon_price_component_name', $price_component_name, $commerce_coupon); $line_item = commerce_coupon_line_item_new($commerce_coupon, $commerce_order->order_id->value()); @@ -247,10 +278,10 @@ function commerce_coupon_action_create_coupon_line_item($commerce_coupon, $comme // Add the base price to the components array. - if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'commerce_coupon_' . $commerce_coupon->type)) { + if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), $price_component_name)) { $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add( $line_item_wrapper->commerce_unit_price->value(), - 'commerce_coupon_' . $commerce_coupon->type, + $price_component_name, $line_item_wrapper->commerce_unit_price->value(), TRUE, FALSE diff --git a/commerce_coupon.rules_defaults.inc b/commerce_coupon.rules_defaults.inc new file mode 100644 index 0000000..8f039e1 --- /dev/null +++ b/commerce_coupon.rules_defaults.inc @@ -0,0 +1,53 @@ + array( + 'type' => 'commerce_coupon', + 'label' => t('Coupon'), + ), + 'number_of_redemptions' => array( + 'type' => 'integer', + 'label' => t('Number Of Redemptions'), + ), + )); + $rule->label = t('Coupon Validation: Set the coupon as invalid if number of uses is reached'); + $rule + ->condition('entity_has_field', array('entity:select' => 'commerce_coupon', 'field' => 'commerce_coupon_number_of_uses')) + ->condition(rules_and()->condition( + rules_or()->condition('data_is', array('data:select' => 'commerce_coupon:commerce-coupon-number-of-uses', 'op' => '<', 'value:select' => 'number-of-redemptions')) + ->condition('data_is', array('data:select' => 'commerce_coupon:commerce-coupon-number-of-uses', 'value:select' => 'number-of-redemptions')) + ) + ); + $rule->action('drupal_message', array( + 'message' => 'Sorry, the maximum number of redemptions for this coupon has been reached.', + 'type' => 'error', + )); + $rule->action('commerce_coupon_action_is_invalid_coupon', array()); + $rules['commerce_coupon_basic_validate_uses_of_coupon_component'] = $rule; + + // Reaction on the validation event for check uses of coupons: + $rule = rules_reaction_rule(); + $rule->label = t('Coupon Validation: Check the number of redemptions'); + $rule->active = TRUE; + + $rule + ->event('commerce_coupon_validate') + ->condition('entity_has_field', array('entity:select' => 'coupon', 'field' => 'commerce_coupon_number_of_uses')) + ->action('commerce_coupon_action_get_coupon_uses', array('commerce_coupon:select' => 'coupon')) + ->action('component_commerce_coupon_basic_validate_uses_of_coupon_component', array( + 'commerce_coupon:select' => 'coupon', + 'number_of_redemptions:select' => 'number-of-uses' + )); + $rules['commerce_coupon_basic_validate_uses_of_coupon'] = $rule; + + return $rules; +} diff --git a/commerce_coupon_ui.module b/commerce_coupon_ui.module index 1ee9116..734f087 100644 --- a/commerce_coupon_ui.module +++ b/commerce_coupon_ui.module @@ -35,7 +35,7 @@ function commerce_coupon_ui_menu() { 'access arguments' => array('create'), ); - foreach (commerce_coupon_get_types () as $type => $coupon_type) { + foreach (commerce_coupon_get_types() as $type => $coupon_type) { $items['admin/commerce/coupons/add/' . strtr($type, array('_' => '-'))] = array( 'title' => 'Create !name', 'title arguments' => array('!name' => $coupon_type->label),