Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lathan’s picture

Status: Active » Needs review
FileSize
2.68 KB

Here is a patch

lathan’s picture

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

pcambra’s picture

Status: Needs review » Needs work

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

lathan’s picture

Just attaching fix of last patch.

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

pcambra’s picture

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!

pcambra’s picture

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" ]
        }
      }
    ]
  }
}
pcambra’s picture

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

lobo235’s picture

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!

valante’s picture

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?

valante’s picture

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" : [  ] }
    ]
  }
}
lathan’s picture

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.

lathan’s picture

Status: Needs work » Needs review

update status.

pcambra’s picture

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()

idflood’s picture

Status: Needs work » Needs review
FileSize
2.11 KB

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

brunorios1’s picture

Status: Needs review » Reviewed & tested by the community

#14 + #6 worked here.

pcambra’s picture

Status: Reviewed & tested by the community » Fixed

And committed! thanks

Status: Fixed » Closed (fixed)

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

bmx269’s picture

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

pcambra’s picture

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

bmx269’s picture

Ok, Gotcha. Thanks.

caschbre’s picture

Issue summary: View changes

I realize this is an older issue but before creating a new one I wanted to check and see if this applies to 2.x?