1

I need something which could seems a little strange, but it's a client request...

I have a site selling products using the Commerce module. All is working fine.

I need to allow the visitor to request a Discount Coupon in the following way:

  1. The buyer see the full display for the product.
  2. The product has a price of: 10,00 $
  3. Bellow the price there is a form to allow the visitor to request a Discount Coupon
  4. This form has 2 fields: one is email and the other one is called: 'I want to pay'. The visitor enter the price he/she wants to pay: 8,00 $ and the email.
  5. The form is sent and some calculation are done based in: if the value of 'I want to pay' is >= than the price of the product (10,00 $). Then the condition is true and the visitor receive an email with a Discount Coupon
  6. The buyer enters this Discount Coupon and pay 8,00 $ for the product.

Can I do this with Rules and Commerce Coupon. Do I need some other module?

1

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,

  1. 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;
  }
}
  1. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.