I'm updating a custom field within a product to contain a discount code. The code is generated and the custom field updated using the onBeforeOrderComplete event. All works well, apart from the order email:
- The order email doesn't contain the discount code (the field is just blank).
- Checking the product immediately after in the backend, the code is in the field.
- Switching the order status back to
processingsends the email with the code in it.
Which makes me think the email is going out before onBeforeOrderComplete is triggered. Is this right?
Here is how I set the custom field:
public function updateProductWithDiscountCode($productId, $discountCode)
{
// GET THE PRODUCT, UPDATE IT AND SAVE
$product = craft()->commerce_products->getProductById($productId);
$product->setContentFromPost(array('discountCode' => $discountCode ));
if (craft()->commerce_products->saveProduct($product)) {
GiftVouchersPlugin::log("Product #$product->id updated with coupon code $discountCode", LogLevel::Info);
}
return TRUE;
}
The plugin work in progress is here: https://github.com/cliveportman/giftVouchers
What should be happening:
- gift voucher product is created and added to the cart
- payment takes place
- when onBeforeOrderComplete is called, if the order contains a gift voucher product, we create a promo and add the discount code to a field within the product
- the order email is sent, containing the discount code
What I think is happening:
- gift voucher product is created and added to the cart
- payment takes place
- the order email is sent, without the discount code
- when onBeforeOrderComplete is called, if the order contains a gift voucher product, we create a promo and add the discount code to a field within the product
Here is a sample of the email template:
{% for item in order.lineItems %}
{% if item.purchasable | length %}
{% set product = item.purchasable.product %}
<div class="cartitem">
<p><strong>{{ item.price | currency(order.currency) }} {{ product.title }}</strong>
<br>
// THIS IS POPULATED WHEN THE PRODUCT IS CREATED SO DISPLAYS FINE
{% if product.recipientName | length %}for {{ product.recipientName }}<br>{% endif %}
// THIS IS EMPTY WHEN THE ORDER EMAIL IS SENT OUT THE FIRST TIME
// BUT IS POPULATED IF SENT OUT THROUGH A LATER STATUS CHANGE
{% if product.discountCode | length %}code {{ product.discountCode }}<br>{% else %}
// THIS IS EMPTY AS WELL
{{ product.content.discountCode }}
// THIS IS MY FALLBACK AT THE MOMENT - SENDING PEOPLE TO THE VIEW ORDER PAGE WHERE I CAN SHOW THEM THE DISCOUNT CODE USING {{ product.discountCode }}
{#<a href="{{ siteUrl }}orders/{{ order.number }}">Click here to view your gift voucher code</a>#}<br>{% endif %}
</p>
</div>
{% endif %}
{% endfor %}
onBeforeOrderComplete? – Luke Holder May 09 '17 at 17:58onBeforeOrderComplete. – Luke Holder May 09 '17 at 18:02product.content.etcin this case? Just guessing! – Jeremy Daalder May 10 '17 at 23:45order.content...is going to help. What I don't understand is why the email is being sent before my product has been updated, when according to my digging around and Luke's comment it shouldn't be doing so. – Clive Portman May 11 '17 at 08:17product.content.whatevsspecifically - I think this is a way of addressing the products content (that may have changed) before it is saved back to the db. Should only take a few seconds to try it anyway! – Jeremy Daalder May 11 '17 at 09:23