/** * Mark the entire order as being redeemed if it contains all redeemed vouchers. * Also, generate any voucher pdfs for items newly added from the admin * * @since 1.2 * @param int $post_id the order id * @param object $post the order */ public function maybe_complete_order($post_id, $post) { // generate any vouchers as needed wc_pdf_product_vouchers()->get_voucher_handler()->generate_voucher_pdf($post_id); $order = wc_get_order($post_id); $voucher_count = 0; // if the order status is not completed, and the entire order has not already been marked as 'voucher redeemed' if (!WC_PDF_Product_Vouchers_Order::vouchers_redeemed($order)) { foreach (WC_PDF_Product_Vouchers_Order::get_vouchers($order) as $voucher) { $voucher_count++; // an unredeemed voucher, bail if (!$voucher->is_redeemed()) { return; } } if ($voucher_count) { // if we made it here, it means this order contains only voucher items, and they are all redeemed WC_PDF_Product_Vouchers_Order::mark_vouchers_redeemed($order, $voucher_count); } } }
/** * Invoked when an order status changes to 'completed', or 'processing' * depending on how WooCommerce is configured. If the order has any * voucher items, the voucher PDF files are generated * * @since 1.2 * @param int $order_id newly created order identifier * @param boolean $force use true to force the voucher to be re-generated regardless of whether it already exists */ public function generate_voucher_pdf($order_id, $force = false) { global $wpdb; $order = wc_get_order($order_id); foreach (WC_PDF_Product_Vouchers_Order::get_vouchers($order) as $voucher) { // voucher has already been generated if ($voucher->file_exists($this->plugin->get_uploads_path()) && !$force) { continue; } // NOTE: I could set the expiration (and voucher number) on the order item meta if I want it displayed in the frontend // set the expiration date if needed if (!$voucher->get_formatted_expiration_date() && $voucher->get_expiry()) { $expiry_from_date = apply_filters('wc_pdf_product_vouchers_expiry_from_date', time(), $order_id, $voucher); $voucher->set_expiration_date($expiry_from_date + $voucher->get_expiry() * 24 * 60 * 60); wc_update_order_item_meta($voucher->get_item_id(), '_voucher_expiration', date('Y-m-d', time() + $voucher->get_expiry() * 24 * 60 * 60)); } // ensure the path that will hold the voucher pdf exists $voucher_pdf_path = $this->plugin->get_uploads_path() . '/' . $voucher->get_voucher_path(); if (!file_exists($voucher_pdf_path)) { @mkdir($voucher_pdf_path, 0777, true); } // is the output path writable? if (!is_writable($voucher_pdf_path)) { $order->add_order_note(sprintf(__("PDF Product Vouchers file permission error: unable to generate voucher %s in %s Please fix the directory permissions and re-generate the voucher.", WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number(), $voucher_pdf_path)); continue; } // try to generate the voucher try { $voucher->generate_pdf($this->plugin->get_uploads_path()); } catch (Exception $exception) { $order->add_order_note(sprintf(__("PDF Product Vouchers: unable to generate voucher %s due to: %s", WC_PDF_Product_Vouchers::TEXT_DOMAIN), $voucher->get_voucher_number(), $exception->getMessage())); } } }