0

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!

Ryan
  • 510
  • 4
  • 16
  • Are you specifically using `woocommerce_order_item_meta_end` for the placement or is utilizing another hook/filter a possibility? – T Paone Nov 04 '19 at 18:10
  • Exactly. Since there needs to be a voucher code for each item ordered I don't think another hook could work. (but am open to it) I'm basing this on the WooCommerce standard hooks: [email confirmation hook visual guide](https://businessbloomer.com/woocommerce-visual-hook-guide-emails/). – Ryan Nov 04 '19 at 19:52

2 Answers2

0

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.

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) {

    // 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>';
    }
}

Resources
email confirmation hook visual guide
wc_get_order_item_meta docs
Filter out unwanted order item meta data from Woocommerce email notifications

Ryan
  • 510
  • 4
  • 16
0

I faced the same problem. As I understand from the source code there is another hook

woocommerce_pre_payment_complete

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

Saman Salehi
  • 1,004
  • 1
  • 12
  • 19