Our store sells software and we’re adding a software voucher code to each purchased. Once the purchase is completed (via the woocommerce_payment_complete
hook) we generate the voucher code and add it to each item purchased via wc_add_order_item_meta method.
Summarized code:
add_filter('woocommerce_payment_complete', 'add_voucher_code');
function add_voucher_code( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
for ($i = 1; $i <= $item['qty']; $i++) {
$voucher_code = 'API request based on order information to get voucher code';
wc_add_order_item_meta($item->get_id(), 'Voucher Code', $voucher_code);
}
}
}
For some reason or another the item custom meta shows up on the order confirmation page, but doesn’t in the confirmation email. (problem 1 slaps forehead) So we’re utilizing the woocommerce_order_item_meta_end
hook to add it to the confirmation email. (wc_get_order_item_meta)
Summarized code:
add_action('woocommerce_order_item_meta_end', 'email_confirmation_display_order_items', 10, 4);
function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
Problem 2 is that added snippet of code shows on the both the order confirmation page (so now it shows twice) and in the order confirmation email. (slaps forehead again)
Current Problem 2 Solution
Right now we solved it by adding an if statement which is suggested here. Like so:
// Only on emails notifications
if( ! (is_admin() || is_wc_endpoint_url() )) {
echo '<div>Voucher Code: '. wc_get_order_item_meta( $item_id, 'Voucher Code') .'</div>';
}
This feels like a band-aid fix and any insight/suggestions would be much appreciated. Thanks!
2
Answers
Since this isn't getting much action I'll put our band-aid fix as the current solution.
Problem 1 Solution
The added item meta data shows on the order confirmation page and does not show on the confirmation email. We solved this by utilizing the
woocommerce_order_item_meta_end
hook to add the extra item meta.Problem 2 Solution
Adding the item meta data via
woocommerce_order_item_meta_end
to the confirmation email also adds it to the confirmation page (visibly duplicating it). We solved this by adding an if statement suggested by LoicTheAztec here.Resources
email confirmation hook visual guide
wc_get_order_item_meta docs
Filter out unwanted order item meta data from Woocommerce email notifications
I faced the same problem.
As I understand from the source code there is another hook
which fixed the issue for me.
Hook woocommerce_payment_complete called after payment made and after order status change and after email trigger so it doesn’t include updated metadata.
Hook woocommerce_pre_payment_complete calls after payment made but before order status change and before email sending. So it worked for me to add metadata into order item and display in email, order confirmation page, admin order view page correctly without using additional code for woocommerce_order_item_meta_end