Discount 7.x.1.0-alpha7
Coupon 7.x.2.0-beta4
I wonder if anyone has implemented this before (in Coupon 7.x.2.0) - I can't find any examples anywhere. I need to create a coupon for a specific discount when an order is made. The coupon code will be used only once by the user who has made the order. The following code creates and saves a coupon:
$coupon = commerce_coupon_create('discount_coupon');
$coupon->code = commerce_coupon_generate_coupon_code(8);
$coupon->commerce_coupon_recipient[LANGUAGE_NONE][0]['target_id'] = $order->uid;
$coupon->commerce_discount_reference[LANGUAGE_NONE][0]['target_id'] = 7;
$coupon->commerce_coupon_conditions[LANGUAGE_NONE][0] = array(
'condition_name' => 'commerce_coupon_usage_evaluate_usage',
'condition_settings' => array(
'max_usage' => 1
)
);
commerce_coupon_save($coupon);
However, when the coupon is applied to an order, errors occur:
Unable to get a data value. Error: Unable to get the data property code as the parent data structure is not set.
and
EntityMetadataWrapperException: Unable to get the data property coupon_id as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /var/www/.../html/profiles/commerce_kickstart/modules/contrib/entity/includes/entity.wrapper.inc).
The above code is something I have derived from various sources including the Coupon 7.x.2.0-beta4 code but I suspect it is missing a wrapper of some description. Can anyone point me in the right direction here?
Thanks,
Comments
Comment #2
jamesh81 CreditAttribution: jamesh81 commentedComment #3
Sardis CreditAttribution: Sardis commented@jamesh81, Are you sure, that here:
$coupon->code = commerce_coupon_generate_coupon_code(8);
everything's okay with function arguments?
Comment #4
jamesh81 CreditAttribution: jamesh81 commentedHi Sardis,
You are correct to flag this up - although it still generated the coupon code fine, the above single argument function call was based on the Coupon 1.x version. 2.x introduced a $type argument. For clarity, the updated code is:
However, the errors still remain when the coupon is added to the cart.
Comment #5
Sardis CreditAttribution: Sardis commentedjamesh81, still the message is "unable to get the data property coupon_id"?
Comment #6
jamesh81 CreditAttribution: jamesh81 commentedStill the exact same errors as in the original post.
Comment #7
Sardis CreditAttribution: Sardis commentedHi again, jamesh81! So? Did you try to ddebug_backtrace() it?
Comment #8
jamesh81 CreditAttribution: jamesh81 commentedHi Sardis,
Sorry for the delay. The errors mentioned above appear to have inexplicably gone away - the reason for the delay in replying was I wanted to do some testing on the dev site and then on the production site to make sure, but it does indeed seem to be working fine now. Voucher code is generated and is redeemable without issues. I can only assume the errors continued for a time as a residual effect of previous experimenting.
Thanks for your input.
Comment #9
jamesh81 CreditAttribution: jamesh81 commented