when juggling with #1159162: reference a coupon with a commercial product i found a code flaw

the idea is that when a commerce_coupon entity is saved, and has no coupon code, some is autogenerated.
this is a key to coupon generation with rules.

some code review suggests that this can't work, because
* what is done in commerce_coupon_save()
* must be done in a hook_entity_presave()
because entity_save() does not care about save callback if entity class has a save callback as crud entity has.

so commerce_coupon_save is never called on a $coupon->save()

Comments

geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new1.98 KB

so the right thing to do is use hook_entity_presave

attached patch does this and fixes #1159162: reference a coupon with a commercial product for me

geek-merlin’s picture

ps: in fact the patch is a 3-liner, it just looks big because a dozen lines had to be indented

geek-merlin’s picture

erps, now it's complete.

fago’s picture

I'd suggest implementing that in your entity class' save() method, then just call parent::save().

fago’s picture

Status: Needs review » Needs work
+function commerce_coupon_basic_entity_presave($coupon, $type) {
+  if ($type == 'commerce_coupon') {

If using the hook, it should be hook_commerce_coupoin_presave(). Still, the superb solution is as outline in #4 .

geek-merlin’s picture

Status: Needs work » Fixed

did it therightway(tm) and committed.

Status: Fixed » Closed (fixed)

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