I am in the process of building a gift card system for a Craft Commerce site. The current process is that the customer will order a gift card and proceed through checkout as with any other item.
The only information passed through the options is the amount, which is used to generate the gift card. The process works perfectly, and the gift card is generated exactly how it is supposed too.
The next step is to modify the line items using the event onBeforeOrderComplete
or onOrderComplete
. I would like to change the options to include the amount, and the gift card code.
This is the process I have at the moment, which is just one of the many variations that I've attempted. Nothing I do seems to change the options on the order.
craft()->on('commerce_orders.onBeforeOrderComplete', function($event) {
$order = $event->params['order'];
$items = $order->lineItems;
foreach( $items as $key => $item ) {
$type = $item->getPurchasable()->getProduct()->getType();
if ($type->handle === 'giftCard') {
$options = $item->options;
$giftcard = new GiftCards_GiftCardModel();
$giftcard->amount = $item->price;
$giftcard->balance = $item->price;
$giftcard->isActive = 1;
craft()->giftCards->saveGiftCard($giftcard);
$options['code'] = $giftcard->code;
$items[$key]->options = $options;
}
}
$order->lineItems = $items;
});
My only other solution is to generate the gift card code when the gift card is added to the cart and change the active status when order is complete. This would solve my problem completely, but it runs the risk of having hundreds or even thousands of gift cards generated and never completely activated.