I think this can be done via Rules and the Commerce Coupon Module. But you may need to write custom Rules condition and event. But first, from your question it is not clear that the form of email and price that you are saying is line items? I am assuming that those are line items and displayed in the Product display. So in that case in a custom module,
- We will use the action "When an order is first paid in full" and create a new custom condition to check if the line items are set or not for the payment made. This can be done via creating a new file as "custom_module.rules.inc". In that file create a new hook as hook_rules_condition_info()
/**
* Implements hook_rules_condition_info()
* @return array $conditions
*/
function custom_module_rules_condition_info() {
$conditions = array();
$conditions['agree_to_mail_coupon'] = array(
'label' => t('Check if user paid for Coupon Code'),
'group' => t('Coupon'),
);
return $conditions;
}
function agree_to_mail_coupon() {
//This conditions only return TRUE or FALSE.
if(your line item check condition) { // Add logic to check for the line items. you will get API from api.drupalcommerce.org
return TRUE;
} else {
return FALSE;
}
}
- Then create a new Event to send the mail. But you need a function to create the coupon code. So via commerce_coupon.module API list you will get commerce_coupon_create or other functions to use. So to create a custom event that will get the email address and the coupon code and send the mail you need the following,
/**
* Implements hook_rules_action_info()
* @return array $actions
*/
function armory_commerce_commission_rules_action_info() {
$actions = array();
$actions['send_coupon_code'] = array(
'label' => t('Send Coupon code'),
//Add your parameters in the parameter array. Given are some demo below
'parameter' => array(
'commerce_line_item' => array(
'type' => 'commerce_line_item',
'label' => t('Line item'),
),
'component_name' => array(
'type' => 'text',
'label' => t('Price component type'),
'description' => t('Price components track changes to prices made during the price calculation process, and they are carried over from the unit price to the total price of a line item. When an order total is calculated, it combines all the components of every line item on the order. When the unit price is altered by this action, the selected type of price component will be added to its data array and reflected in the order total display when it is formatted with components showing. Defaults to base price, which displays as the order Subtotal.'),
'options list' => 'commerce_line_item_price_component_options_list',
'default value' => 'base_price',
),
'round_mode' => array(
'type' => 'integer',
'label' => t('Price rounding mode'),
'description' => t('Round the resulting price amount after performing this operation.'),
'options list' => 'commerce_round_mode_options_list',
'default value' => COMMERCE_ROUND_HALF_UP,
),
),
'group' => t('Coupon Discount'),
);
return $actions;
}
function send_coupon_code($line_item, $component_name, $round_mode) {
//Add your coupon generation code
//Use Drupal_mail to send email to the user
}
I haven't tested this one as it's a unique type of requirement. But this will be a basic logic to solve the issue. You may need to do a fair bit of R&D to get it working. I appreciate if you ask if you found some questions & queries.