Hello,
i would like to know if it possible to attach a coupon to a discount using rules.

I have currently created this rule:

{ "rules_create_5_euro_discount" : {
"LABEL" : "Create 5 Euro Discount",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"DO" : [
{ "entity_create" : {
"USING" : { "type" : "commerce_coupon" },
"PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
}
},
{ "data_set" : {
"data" : [ "entity-created:code" ],
"value" : "5EOFF[site:current-user]"
}
},
{ "data_set" : { "data" : [ "entity-created:type" ], "value" : "discount_coupon" } },
{ "entity_save" : { "data" : [ "entity-created" ] } },
{ "component_rules_attach_5_euro_discount_to_coupon" : { "coupon" : [ "entity-created" ] } },
{ "mail" : {
"to" : "nkostop@gmail.com",
"subject" : "Coupon created",
"message" : "[entity-created:code]",
"from" : "",
"language" : [ "" ]
}
}
]
}
}

And this component :

{ "rules_attach_5_euro_discount_to_coupon" : {
"LABEL" : "Attach 5 euro discount to coupon",
"PLUGIN" : "rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "coupon" : { "label" : "coupon", "type" : "commerce_coupon" } },
"IF" : [
{ "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_discount_reference" } }
],
"DO" : [
{ "list_add" : {
"list" : [ "coupon:commerce-discount-reference" ],
"item" : "3",
"unique" : "1"
}
}
]
}
}

I am using the item "3" because this is the id of the discount.

Am i doing something wrong or is this impossible?

What i am trying to do is to make a unique coupon every time a user checkouts.
Any other opinion appreciated!

Thanks in advance!

CommentFileSizeAuthor
#3 create-coupon.png119.48 KBtfranz
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nkostop created an issue. See original summary.

tfranz’s picture

Did you succeed in using rules to attach a coupon to a discount?

Thank you for any feedback!

tfranz’s picture

FileSize
119.48 KB

These are the only possibilities for Data-set:
Create Coupon

The reference to discount is missing?

marameodesign’s picture

Hi, I am having exactly the same issue, and I have been trying to solve it with:

Create new Entity
> Conditional
>>If: Entity has field -> commerce_discount_reference
>>>Set Data Value entity_created:commerce_discount_reference Value: xxx
Set data value entity:created:code Value: XXX
Save entity

This is a rule that fires if X product is in the cart at checkout.

The problem is that it creates this error:
EntityMalformedException: Missing bundle property on entity of type commerce_coupon. in entity_extract_ids() (line 7946 of /Users/marameo/Sites/possumsonline/includes/common.inc).

And if I look at the eval of the rule I get:

Rule Gift Certificate fires. [edit]
0 ms Rule Gift Certificate fires.
0.895 ms Evaluating the action entity_create. [edit]
1.443 ms Added the provided variable entity_created of type commerce_coupon [edit]
39.913 ms Evaluating the action data_set. [edit]
40.253 ms The condition entity_has_field evaluated to FALSE [edit] <<<<<< SO it doesn't see that the entity has the field!
40.336 ms Evaluating the action entity_save. [edit]
40.389 ms Saved entity_created of type commerce_coupon.
49.427 ms Evaluating the action drupal_message. [edit]
49.642 ms Rule Gift Certificate has fired.

Can anyone help me with this?

Thanks