Hi,

Great module, it seems to be working well for us.

Problem

We have a banner ad on another site for a promotion we are running. Instead of giving the user a coupon they had to retype, I wanted the link to add the coupon / discount directly to their cart. The easiest way to do this is through rules.

The problem was that there is not a relevant action for adding a coupon to a specific order. I tried to use the '%off' action on each of the line items in an order, but this didn't get me anywhere. The other problem is that having a discount without a coupon code gets applied to every user, which is not what I wanted.

Solution

I modified the code to add the action. It was pretty straight forward and seems to be working, so I thought I'd share incase it's useful to anyone else.

Inside commerce_coupon.rules.inc

/*
 * Implements hook_rules_action_info()
 */
function commerce_coupon_rules_action_info() {
  $actions['commerce_coupon_apply_to_order'] = array(
    'label' => t('Apply a coupon to a cart order'),
    'group' => t('Commerce Coupon'),
    'parameter' => array(
      'coupon_code' => array(
        'type' => 'text',
        'label' => t('Coupon Code'),
        ),
      'commerce_order' => array(
        'type' => 'commerce_order',
        'label' => t('Order'),
        ),
      )
    );

  return $actions;
}


/*
 * Rules action callback: apply a coupon to given order
 */
function commerce_coupon_apply_to_order($coupon_code, $commerce_order){
  $error = '';
  commerce_coupon_redeem_coupon_code($coupon_code, $commerce_order, $error);
  if($error != ''){
    drupal_set_message(t('Error adding coupon: ' . $error), 'error');
  }
}

Hope this is useful to someone. Let me know if you decide to include it in your code base

CommentFileSizeAuthor
apply_coupon.diff1.42 KBjwa
commerce_coupon_apply_rules.png34.37 KBjwa

Comments

lionguard’s picture

You may want to use something like this for the action instead:

/*
 * Rules action callback: apply a coupon to given order
 */
function commerce_coupon_apply_to_order($coupon_code, $commerce_order){

  drupal_page_is_cacheable(FALSE);
 $user = $GLOBALS['user'];

  // Create new cart if no order exists for this user.
  if (!$commerce_order) {
    $commerce_order = commerce_cart_order_new($user->uid);
  }

  // Apply coupon to order.
  $error = '';
  $coupon = commerce_coupon_redeem_coupon_code($coupon_code, $commerce_order, $error);

  if ($coupon) {
    // Reload the order so it is not out of date.
    $commerce_order = commerce_order_load($commerce_order->order_id);

    // Recalculate discounts.
    commerce_cart_order_refresh($commerce_order);
  }

  if (!$error) {
    // If a coupon was invalidated during the cart refresh (e.g. if its
    // discounts failed their conditions), an error message will have been
    // set.
    $error = &drupal_static('commerce_coupon_error_' . strtolower($coupon->code));
  }

  if (!$error) {
    // Allow modules/rules to act when a coupon has been successfully added
    // to the cart.
    rules_invoke_all('commerce_coupon_applied_to_cart', $coupon, $commerce_order);
  }

}
HamidReza’s picture

tnx for this Rules...But it can only work with One Coupon Code.
how we can use unlimited Coupon code with this Rules action?