/**
  * Pluggable function to render the frontend product page voucher fields
  *
  * @since 1.2
  * @param WC_Product $product the voucher product
  */
 function wc_pdf_product_vouchers_render_product_voucher_fields($product)
 {
     if ($product->is_type('variable')) {
         foreach ($product->get_children() as $variation_product_id) {
             $products[] = wc_get_product($variation_product_id);
         }
     } else {
         $products[] = $product;
     }
     foreach ($products as $product) {
         $voucher = WC_PDF_Product_Vouchers_Product::get_voucher($product);
         if ($voucher) {
             $fields = $voucher->get_user_input_voucher_fields();
             $images = $voucher->get_image_urls();
             if ($fields || $images) {
                 // load the template file
                 wc_get_template('single-product/product-voucher.php', array('product' => $product, 'product_id' => isset($product->variation_id) ? $product->variation_id : $product->id, 'voucher' => $voucher, 'fields' => $fields, 'images' => $images), '', wc_pdf_product_vouchers()->get_plugin_path() . '/templates/');
             }
         }
     }
 }
 /**
  * Add any user-supplied voucher field data to the cart item data, to
  * set in the session
  *
  * @since 1.2
  * @param array $cart_item_data associative-array of name/value pairs of cart item data
  * @param int $product_id the product identifier
  * @param int $variation_id optional product variation identifer
  *
  * @return array associative array of name/value pairs of cart item
  *         data to set in the session
  */
 public function add_cart_item_voucher_data($cart_item_data, $product_id, $variation_id)
 {
     $_product_id = $variation_id ? $variation_id : $product_id;
     $product = wc_get_product($_product_id);
     // is this a voucher product?
     if (WC_PDF_Product_Vouchers_Product::has_voucher($product)) {
         $voucher = WC_PDF_Product_Vouchers_Product::get_voucher($product);
         // record the voucher id
         $cart_item_data['voucher_id'] = $voucher->id;
         // set the selected voucher image id, or default to the main one if the voucher was added from the catalog
         $cart_item_data['voucher_image_id'] = isset($_POST['voucher_image'][$_product_id]) ? $_POST['voucher_image'][$_product_id] : $voucher->image_id;
         // set any user-input fields, which will end up in the order item meta data (which can be displayed on the frontend)
         $fields = $voucher->get_user_input_voucher_fields();
         foreach ($fields as $field) {
             if (isset($_POST[$field['name']][$_product_id])) {
                 $cart_item_data['voucher_item_meta_data'][$field['name']] = $_POST[$field['name']][$_product_id];
             }
         }
         // add a random so that multiple of the same product can be added to the cart when "sold individually" is enabled
         $cart_item_data['voucher_random'] = uniqid('voucher_');
     }
     return $cart_item_data;
 }
 /**
  * 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]);
             }
         }
     }
 }