1

It is practically impossible for me to set the fixed amount of a coupon created with rules. The coupon is generated allright with a coupon code but the fixed amount value is always empty, I can only edit the generated coupon and set the fixed amount manually. I am referring to these issues which seemed to have never been resolved:

Edit: I know that Commerce GC exists but it is buggy and not production ready (breaks my site during checkout) and the bug issues havent been resolved.

Here is my exported rule:

{ "rules_send_a_gift_certificate" : {
"LABEL" : "Send A Gift Certificate",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "commerce_order", "rules", "commerce_coupon", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
  { "commerce_order_contains_product" : {
      "commerce_order" : [ "commerce_order" ],
      "product_id" : "BGC-1-8",
      "operator" : "\u003E=",
      "value" : "1"
    }
  }
],
"DO" : [
  { "entity_create" : {
      "USING" : { "type" : "commerce_coupon" },
      "PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
    }
  },
  { "data_set" : {
      "data" : [ "entity-created:commerce-coupon-code" ],
      "value" : "ord[commerce-order:order-id]ers"
    }
  },
  { "data_set" : { "data" : [ "entity-created:type" ], "value" : "commerce_coupon_fixed" } },
  { "data_set" : { "data" : [ "entity-created:is-active" ], "value" : 1 } },
  { "data_set" : {
      "data" : [ "entity-created:commerce-coupon-number-of-uses" ],
      "value" : "1"
    }
  },
  { "entity_save" : { "data" : [ "entity-created" ] } },
  { "commerce_coupon_action_create_coupon_line_item" : {
      "USING" : {
        "commerce_coupon" : [ "entity-created" ],
        "commerce_order" : [ "commerce_order" ],
        "amount" : "100.00",
        "component_name" : "commerce_coupon_fixed_ord1ers",
        "currency_code" : "NGN"
      },
      "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
    }
  },
  { "data_set" : {
      "data" : [ "commerce-coupon-line-item:commerce-total:amount" ],
      "value" : "100"
    }
  },
  { "entity_save" : { "data" : [ "commerce-coupon-line-item" ] } },
  { "LOOP" : {
      "USING" : { "list" : [ "commerce-order:commerce-line-items" ] },
      "ITEM" : { "list_item" : "Current list item" },
      "DO" : [
        { "mail" : {
            "to" : "[list-item:field-gift-recipient-email]",
            "subject" : "[list-item:field-gift-sender-name] has sent you a Gift Certificate",
            "message" : "Hello [list-item:field-gift-recipient-name], [list-item:field-gift-sender-name] has sent you a \u20a6 [entity-created:commerce-coupon-fixed-amount] Gift Certificate to celebrate your birthday. Your Gift certificate code is: [entity-created:commerce-coupon-code]. Please note that your Gift certificate may only be used once, please spend the full amount in one purchase, any balance remaining is non-refundable. Happy birthday celebration!",
            "language" : [ "" ]
          }
        }
      ]
    }
  }
]
}
}

1 Answer 1

1

After much digging, i was able to resolve this using a rules component. The trick is, after generating your coupon code using rules, that is,

event: completing the checkout process

condition: your condition (e.g order contains a particular product etc)

action: create new entity => commerce coupon action: set data value (do this for the coupon code, max number of uses, coupon type (human readable name), etc) action: save entity => (your created entity)

action: create new entity => commerce line item => commerce coupon (set your values) action: save entity => (your newly created coupon line item)

create component:

component plugin: rule name: name your component here (e.g "set gift coupon value") data type: commerce coupon label: (give a label that you can identify it easily with) machine name: (give a machine name that you can identify easily) save component.

Under conditions: entity has field: (identify your coupon fixed amount field here e.g "coupon-fixed-value-component") action: set a data value => (here you identify the fixed amount coupon field and then set the value you want) save component

Come back to your earlier created rule,

add loop: (it should automatically capture that you want to loop into the commerce line items e.g "commerce-order:commerce-list-item" save loop inside the loop, add action = > (here you'll select the component you created, it should automatically detect that you want to fire this component on your created coupon entity initially created within this rule e.g "entity-created" save the action.

perform any other action if you want, like send email to referenced user in the gift card.

That would do. each time you complete the checkout for the product referenced in the rules condition, a gift coupon will be created and the amount would be set based on your rules component action value.

Limitation: You have to do this whole process for every different amount of gift coupon you create.

Possible improvement: Allow a user to specify the amount at product creation time, then use rules to capture this amount and then set the coupon value based on the set amount of the product

More improvements welcome..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.