/**
  * Make sure the user-input voucher fields are persisted to the database in the
  * order item meta.  This is called during the checkout process for each
  * cart item added to the order.  Requires WooCommerce 2.0+
  *
  * @since 1.2
  * @param int $item_id item identifier
  * @param array $values array of data representing a cart item
  */
 public function add_order_item_meta($item_id, $values)
 {
     // is this a voucher product?
     if (isset($values['voucher_id'])) {
         $voucher = new WC_Voucher($values['voucher_id']);
         wc_add_order_item_meta($item_id, '_voucher_image_id', $values['voucher_image_id']);
         wc_add_order_item_meta($item_id, '_voucher_id', $values['voucher_id']);
         wc_add_order_item_meta($item_id, '_voucher_redeem', array_pad(array(), $values['quantity'], null));
         wc_add_order_item_meta($item_id, '_voucher_number', WC_PDF_Product_Vouchers_Voucher::generate_voucher_number());
         // set any user-input fields to the order item meta data (which can be displayed on the frontend)
         // ie recipient_name, message
         if (isset($values['voucher_item_meta_data'])) {
             foreach ($values['voucher_item_meta_data'] as $name => $value) {
                 if ($voucher->is_user_input_type_field($name) && $value) {
                     // make sure any max length rules are imposed
                     if ($voucher->get_user_input_field_max_length($name)) {
                         $value = substr($value, 0, $voucher->get_user_input_field_max_length($name));
                     }
                     wc_add_order_item_meta($item_id, __($voucher->get_field_label($name), WC_PDF_Product_Vouchers::TEXT_DOMAIN), $value);
                 }
             }
         }
     }
 }
 /**
  * Called when an order is updated from the admin, creates a new voucher if
  * a voucher item was added, and updates the voucher expiration and redeem
  * item meta.
  *
  * @since 1.2
  * @param int $post_id the post identifier
  * @param object $post the order post object
  *
  * @return array order item data to persist
  */
 public function process_shop_order_meta($post_id, $post)
 {
     // get the order
     $order = wc_get_order($post_id);
     $order_items = $order->get_items();
     // loop through any order items by id
     if (isset($_POST['order_item_id'])) {
         foreach ($_POST['order_item_id'] as $item_id) {
             $item_id = absint($item_id);
             if (!isset($order_items[$item_id]) || !$order_items[$item_id]) {
                 continue;
             }
             $order_item = $order_items[$item_id];
             $product_id = $order_item['variation_id'] ? $order_item['variation_id'] : $order_item['product_id'];
             $product = wc_get_product($product_id);
             // if we have a voucher product, but no voucher set for the order item, this is likely an item newly added from the admin, so create a default voucher
             if ($product && $product->is_downloadable() && WC_PDF_Product_Vouchers_Product::has_voucher($product) && (!isset($order_item['voucher_id']) || !$order_item['voucher_id'])) {
                 $voucher = WC_PDF_Product_Vouchers_Product::get_voucher($product);
                 $voucher_number = WC_PDF_Product_Vouchers_Voucher::generate_voucher_number();
                 wc_add_order_item_meta($item_id, '_voucher_image_id', $voucher->get_image_id());
                 wc_add_order_item_meta($item_id, '_voucher_id', $voucher->id);
                 wc_add_order_item_meta($item_id, '_voucher_redeem', array_pad(array(), $order_item['qty'], null));
                 // TODO: need to handle the order item quantity being changed from the admin
                 wc_add_order_item_meta($item_id, '_voucher_number', $voucher_number);
                 // if download permissions have already been granted, grant permission to the newly created voucher
                 if (isset($order->download_permissions_granted[0]) && 1 == $order->download_permissions_granted[0]) {
                     wc_downloadable_file_permission('wc_vouchers_' . $voucher_number, $product_id, $order);
                 }
             }
             if (isset($_POST['voucher_expiration'][$item_id])) {
                 wc_update_order_item_meta($item_id, '_voucher_expiration', $_POST['voucher_expiration'][$item_id]);
             }
             if (isset($_POST['voucher_redeem'][$item_id])) {
                 wc_update_order_item_meta($item_id, '_voucher_redeem', $_POST['voucher_redeem'][$item_id]);
             }
         }
     }
 }