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
| Comment | File | Size | Author |
|---|---|---|---|
| apply_coupon.diff | 1.42 KB | jwa | |
| commerce_coupon_apply_rules.png | 34.37 KB | jwa |
Comments
Comment #1
lionguard commentedYou may want to use something like this for the action instead:
Comment #2
HamidReza commentedtnx for this Rules...But it can only work with One Coupon Code.
how we can use unlimited Coupon code with this Rules action?