Download & Extend

Limit number of coupons applied to the order.

Project:Commerce Coupon
Version:7.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

Should allow the admin user to select how many coupons that want to allow against one order.

Comments

#1

Status:active» needs review

Here is a patch

AttachmentSize
1538158.1-limit-coupons-against-order.patch 2.68 KB

#2

Rerolled with fix to white space issue and changed cart to order in some paces that made more sense with descriptions.

AttachmentSize
1538158.3-limit-coupons-against-order.patch 2.63 KB

#3

Status:needs review» needs work

I like the idea, but can't you do the same thing comparing by rules?

#4

Just attaching fix of last patch.

Looked at doing it via rules but looked a little over kill... will rework this to use rules.

AttachmentSize
1538158.4-limit-coupons-against-order.patch 2.63 KB

#5

Don't think that we should add behaviors that are not configurable by rules, not a huge fan of random configuration settings, I'll review the patch and see if we can easily integrate it or if we can do it already with some rules config.

Thanks!

#6

Ok, came up with a rule to handle this. This won't get into commerce coupon itself, but we may want to have new action called "get number of coupons applied to an order"

Component

{ "rules_set_the_coupon_invalid_if_max_uses_of_coupon_is_reached" : {
    "LABEL" : "Set the coupon invalid if max uses of coupon is reached",
    "PLUGIN" : "rule",
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "USES VARIABLES" : {
      "commerce_coupon" : { "label" : "Coupon", "type" : "commerce_coupon" },
      "number_of_uses" : { "label" : "Number of uses of the coupon", "type" : "integer" }
    },
    "IF" : [
      { "data_is" : { "data" : [ "number-of-uses" ], "op" : "\u003E", "value" : "1" } }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Sorry, the maximum number of coupons for this order has been reached.",
          "type" : "error"
        }
      },
      { "commerce_coupon_action_is_invalid_coupon" : [] }
    ]
  }
}

And main rule

{ "rules_max_number_of_coupons_per_order" : {
    "LABEL" : "Max number of coupons per order",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "commerce_coupon", "rules" ],
    "ON" : [ "commerce_coupon_validate" ],
    "DO" : [
      { "commerce_coupon_action_get_coupons_for_order" : {
          "USING" : { "commerce_order" : [ "commerce_order" ] },
          "PROVIDE" : { "order_coupons" : { "order_coupons" : "Coupons attached to this order" } }
        }
      },
      { "variable_add" : {
          "USING" : { "type" : "integer", "value" : "1" },
          "PROVIDE" : { "variable_added" : { "variable_added" : "Added variable" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "order-coupons" ] },
          "ITEM" : { "list_item" : "Current list item" },
          "DO" : [
            { "data_calc" : {
                "USING" : { "input_1" : [ "variable-added" ], "op" : "+", "input_2" : "1" },
                "PROVIDE" : { "result" : { "result" : "Calculation result" } }
              }
            },
            { "data_set" : { "data" : [ "variable-added" ], "value" : [ "result" ] } }
          ]
        }
      },
      { "component_rules_set_the_coupon_invalid_if_max_uses_of_coupon_is_reached" : {
          "commerce_coupon" : [ "coupon" ],
          "number_of_uses" : [ "variable-added" ]
        }
      }
    ]
  }
}

#7

Marking this #1341384: One Coupon per Order as duplicated of this one.

#8

Regarding the Rule in #6, I cannot get the rule to import because it gives the following error message:

Integrity check for the imported configuratoin failed. Error message: Unknown action commerce_coupon_action_get_coupons_for_order

The component imported without any issues but I can't get the rule to import. Can anyone provide any help on this? I really need to be able to limit the customer to one coupon per order.

Thanks!

#9

I also need this functionality urgently.

What I tried to do:

- Set up a rule on Coupon validation
- Loop over line items
-- If line item is a coupon, make coupon invalid

For some reason, none of the line items get recognized as type "Coupon"? Is this a bug, or am I missing something?

#10

Solved!

Only works with one coupon, though. :)

{ "rules_no_double_coupons" : {
    "LABEL" : "No double coupons",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_validate" ],
    "IF" : [
      { "entity_has_field" : {
          "entity" : [ "commerce-order" ],
          "field" : "commerce_coupon_order_reference"
        }
      },
      { "NOT data_is_empty" : { "data" : [ "commerce-order:commerce-coupon-order-reference" ] } }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Sorry, you cannot apply more than one coupon to this order.",
          "type" : "error"
        }
      },
      { "commerce_coupon_action_is_invalid_coupon" : [  ] }
    ]
  }
}

#11

There was a bit of an over site in the recent refactor removing the action commerce_coupon_action_get_coupons_for_order this patch restores that and allows the rule out lined in #6 to function once again.

AttachmentSize
1538158.11-limit-coupons-against-order.patch 2.05 KB

#12

Status:needs work» needs review

update status.

#13

Status:needs review» needs work

+++ b/commerce_coupon.module
@@ -1065,3 +1065,34 @@ function commerce_coupon_commerce_cart_order_refresh($order_wrapper) {
+    $coupons[] = $coupon_wrapper->value();

I think we should filter out status = 0 coupons from here.
Maybe an Entity Field Query makes more sense here?

+++ b/commerce_coupon.module
@@ -1065,3 +1065,34 @@ function commerce_coupon_commerce_cart_order_refresh($order_wrapper) {
+  return array('order_coupons' => commerce_coupon_get_coupons_in_order($commerce_order->order_number));

We should use order_id, order number might be equal or not to the id

+++ b/commerce_coupon.rules.inc
@@ -117,6 +117,24 @@ function commerce_coupon_rules_action_info() {
+        'label' => 'Coupons attached to this order',

Missing t()

#14

Status:needs work» needs review

Thanks for this rule. Here is the same patch as #11 with the corrections from #13.

AttachmentSize
limit_coupons_against_order-1538158-14.patch 2.11 KB

#15

Status:needs review» reviewed & tested by the community

#14 + #6 worked here.

#16

Status:reviewed & tested by the community» fixed

And committed! thanks

#17

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

#18

I can not find the rule if this was committed, has it been committed to dev?

#19

The action is just there.
http://drupalcode.org/project/commerce_coupon.git/blob/refs/heads/7.x-1....

As it is mentioned in #6 already, the complete rule does not belong to the module, but you can build it using this action quite straightforward

#20

Ok, Gotcha. Thanks.

nobody click here