/**
  * Dispatch the email(s)
  *
  * @since 1.2
  * @param int $order_id order identifier
  */
 public function trigger($order_id)
 {
     // nothingtodohere
     if (!$order_id || !$this->is_enabled()) {
         return;
     }
     // only dispatch the voucher recipient email once, unless we're being called from the Voucher Recipient email order action
     if (get_post_meta($order_id, '_wc_pdf_product_vouchers_voucher_recipient_email_sent', true) && !(isset($_POST['wc_order_action']) && 'send_email_wc_pdf_product_vouchers_voucher_recipient' == $_POST['wc_order_action'])) {
         return;
     }
     $order = wc_get_order($order_id);
     $this->find[] = '{billing_first_name}';
     $this->replace[] = $order->billing_first_name;
     $this->find[] = '{billing_last_name}';
     $this->replace[] = $order->billing_last_name;
     // foreach voucher item in this order, if it contains a recipient email,
     //  add the voucher to those being sent to that recipient.
     // foreach voucher recipient, send an email with any and all vouchers
     $recipient_emails = array();
     $order_items = $order->get_items();
     if (count($order_items) > 0) {
         foreach ($order_items as $order_item_id => $item) {
             if ($item['product_id'] > 0 && isset($item['voucher_id']) && $item['voucher_id']) {
                 $voucher = new WC_Voucher($item['voucher_id'], $order->id, $item, $order_item_id);
                 if ($voucher->get_recipient_email() && $voucher->file_exists(wc_pdf_product_vouchers()->get_uploads_path())) {
                     if (!isset($recipient_emails[$voucher->get_recipient_email()])) {
                         $recipient_emails[$voucher->get_recipient_email()] = array('count' => 0, 'message' => '', 'recipient_name' => $voucher->get_recipient_name());
                     }
                     $recipient_emails[$voucher->get_recipient_email()]['count']++;
                     // message to the recipient?
                     if ($voucher->get_message()) {
                         if ('' === $recipient_emails[$voucher->get_recipient_email()]['message']) {
                             $recipient_emails[$voucher->get_recipient_email()]['message'] = $voucher->get_message();
                         } elseif ($recipient_emails[$voucher->get_recipient_email()]['message'] != $voucher->get_message()) {
                             // guard against the admitedly edge case of multiple vouchers with different messages
                             //  being sent to the same recipient, by just not displaying a message.  Cause it would
                             //  probably look odd to have a bunch of different messages in the same email
                             $recipient_emails[$voucher->get_recipient_email()]['message'] = null;
                         }
                     }
                 }
             }
         }
     }
     foreach ($recipient_emails as $recipient_email => $data) {
         $this->object = array('order' => $order, 'recipient_email' => $recipient_email, 'voucher_count' => $data['count']);
         $this->message = $data['message'];
         $this->recipient_name = $data['recipient_name'];
         $this->recipient = $recipient_email;
         $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
     }
     // record the fact that the vouchers have been sent
     update_post_meta($order_id, '_wc_pdf_product_vouchers_voucher_recipient_email_sent', true);
 }
    /**
     * Order vouchers meta box
     *
     * Displays the order vouchers meta box - for showing and modifying
     * individual vouchers attached to the order
     *
     * @since 1.2
     */
    public function vouchers_meta_box($post)
    {
        $order = wc_get_order($post->ID);
        $order_items = $order->get_items();
        ?>
		<div class="woocommerce_order_vouchers_wrapper">
			<table cellpadding="0" cellspacing="0" class="woocommerce_order_vouchers">
				<thead>
					<tr>
						<th class="thumb" width="1%"><?php 
        _e('Voucher', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="voucher_number"><?php 
        _e('Number', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="sku"><?php 
        _e('SKU', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="data"><?php 
        _e('Data', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="expires"><?php 
        _e('Expires', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="qty"><?php 
        _e('Qty', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</th>
						<th class="redeem" style="white-space:nowrap;"><?php 
        _e('Mark Redeemed', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
&nbsp;<a class="tips" data-tip="<?php 
        _e('Mark the dates that any vouchers are redeemed.  Marking all vouchers redeemed will complete the order.', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
" href="#">[?]</a></th>
					</tr>
				</thead>
				<tbody id="order_vouchers_list">

					<?php 
        if (count($order_items) > 0) {
            foreach ($order_items as $item_id => $item) {
                // only voucher items
                if (!isset($item['voucher_id'])) {
                    continue;
                }
                $item['voucher_redeem'] = maybe_unserialize($item['voucher_redeem']);
                $voucher = new WC_Voucher($item['voucher_id'], $post->ID, $item, $item_id);
                if (isset($item['variation_id']) && $item['variation_id'] > 0) {
                    $_product = wc_get_product($item['variation_id']);
                } else {
                    $_product = wc_get_product($item['product_id']);
                }
                // get any user-supplied voucher data (this includes product variation data and user-entered fields like recipient or message)
                $voucher_data = array();
                if (isset($_product->variation_data)) {
                    $voucher_data = $_product->variation_data;
                }
                $voucher_data = array_merge($voucher_data, $voucher->get_user_input_data());
                ?>
						<tr class="item" rel="<?php 
                echo $item_id;
                ?>
">
							<td class="thumb">
								<a href="<?php 
                echo esc_url(admin_url('post.php?post=' . $voucher->id . '&action=edit'));
                ?>
" class="tips" data-tip="<?php 
                echo '<strong>' . __('Voucher ID:', WC_PDF_Product_Vouchers::TEXT_DOMAIN) . '</strong> ' . $voucher->id;
                ?>
"><?php 
                echo $voucher->get_image();
                ?>
</a>
								<?php 
                if ($voucher->file_exists(WC_PDF_Product_Vouchers::get_uploads_path())) {
                    ?>
									<a href="<?php 
                    echo esc_url(add_query_arg(array('action' => 'download', 'product_id' => $item['product_id'], 'item_id' => $item_id, 'voucher_id' => $item['voucher_id'])));
                    ?>
"><?php 
                    esc_html_e('Download', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
                    ?>
</a>
								<?php 
                }
                ?>
							</td>
							<td class="voucher_number" width="1%">
								<?php 
                echo $voucher->get_voucher_number();
                ?>
							</td>
							<td class="sku" width="1%">
								<?php 
                if ($_product->sku) {
                    echo $_product->sku;
                } else {
                    echo '-';
                }
                ?>
							</td>
							<td class="data">

								<?php 
                echo $item['name'];
                ?>
								<?php 
                if (!empty($voucher_data)) {
                    echo '<br/>' . wc_get_formatted_variation($voucher_data, true);
                }
                ?>
							</td>

							<td class="expires" style="width:auto;">
								<input type="text" name="voucher_expiration[<?php 
                echo $item_id;
                ?>
]" id="voucher_expiration_<?php 
                echo $item_id;
                ?>
" maxlength="10" value="<?php 
                echo $voucher->expiration_date ? date("Y-m-d", $voucher->expiration_date) : '';
                ?>
" class="date-picker-field" />
							</td>

							<td class="qty" width="1%">
								<?php 
                echo $item['qty'];
                ?>
							</td>

							<td class="redeem" width="1%">
								<?php 
                foreach ($item['voucher_redeem'] as $i => $redeem) {
                    ?>
									<input type="text" name="voucher_redeem[<?php 
                    echo $item_id;
                    ?>
][<?php 
                    echo $i;
                    ?>
]" id="voucher_redeem_<?php 
                    echo $item_id . '_' . $i;
                    ?>
" class="voucher_redeem date-picker-field" maxlength="10" style="width:85px;" value="<?php 
                    echo $redeem;
                    ?>
" class="date-picker-field" />
									<?php 
                }
                ?>
							</td>

						</tr>
					<?php 
            }
        }
        ?>
				</tbody>
			</table>
		</div>

		<p class="buttons buttons-alt">
			<button type="button" class="button redeem_all_vouchers"><?php 
        _e('Redeem All &uarr;', WC_PDF_Product_Vouchers::TEXT_DOMAIN);
        ?>
</button>
		</p>
		<div class="clear"></div>
		<?php 
    }