/**
* Generate PDF for Voucher
*
* Handles to Generate PDF on run time when
* user will execute the url which is sent to
* user email with purchase receipt
*
* @package WooCommerce - PDF Vouchers
* @since 1.0.0
*/
function woo_vou_process_product_pdf($productid, $orderid, $item_id = '', $orderdvoucodes = array(), $pdf_args = array())
{
$prefix = WOO_VOU_META_PREFIX;
global $current_user, $woo_vou_model;
//model class
$model = $woo_vou_model;
// If pdf argument is not array then make it array
$pdf_args = !is_array($pdf_args) ? (array) $pdf_args : $pdf_args;
// Taking voucher key
if (!empty($pdf_args['pdf_vou_key'])) {
$pdf_vou_key = $pdf_args['pdf_vou_key'];
} else {
$pdf_vou_key = isset($_GET['key']) ? $_GET['key'] : '';
}
if (!empty($productid) && !empty($orderid)) {
// Check product id & order id are not empty
//get all voucher details from order meta
$allorderdata = $model->woo_vou_get_all_ordered_data($orderid);
// Creating order object for order details
$woo_order = new WC_Order($orderid);
$woo_order_details = $woo_order;
$items = $woo_order_details->get_items();
foreach ($items as $key => $item) {
$qty = $items[$key]['qty'];
}
// Getting product name
$woo_product_details = $model->woo_vou_get_product_details($orderid, $items);
// get product
$product = wc_get_product($productid);
// if product type is variable then $productid will contain variation id
$variation_id = $productid;
// check if product type is variable then we need to take parent id of variation id
if ($product->is_type('variation') || $product->is_type('variable')) {
// productid is variation id in case of variable product so we need to take actual product id
$woo_variation = new WC_Product_Variation($productid);
$productid = !empty($woo_variation->id) ? $woo_variation->id : $variation_id;
}
$product = wc_get_product($productid);
$duree = get_the_terms($productid, 'pa_duree');
$args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $productid);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$image = wp_get_attachment_image($attachment->ID, 'full');
}
}
// Getting order details
$buyer_email = $woo_order_details->billing_email;
$buyer_fname = $woo_order_details->billing_first_name;
$buyer_lname = $woo_order_details->billing_last_name;
$buyer_fullname = $buyer_fname . ' ' . $buyer_lname;
$productname = isset($woo_product_details[$variation_id]) ? $woo_product_details[$variation_id]['product_name'] : '';
//Added in version 2.3.7 for sold indivdual bug
$item_key = $item_id;
if (empty($item_key)) {
$item_key = isset($woo_product_details[$variation_id]) ? $woo_product_details[$variation_id]['item_key'] : '';
}
$variation_data = $model->woo_vou_get_variation_data($woo_order, $item_key);
$productname = $productname . $variation_data;
//Get recipient fields
$recipient_data = $model->woo_vou_get_recipient_data_using_item_key($woo_order, $item_key);
$productprice = !empty($woo_product_details[$variation_id]['product_formated_price']) ? $woo_product_details[$variation_id]['product_formated_price'] : '';
//Get voucher codes
$voucher_codes = wc_get_order_item_meta($item_id, $prefix . 'codes');
//get all voucher details from order meta
$allvoucherdata = isset($allorderdata[$productid]) ? $allorderdata[$productid] : array();
//how to use the voucher details
$howtouse = isset($allvoucherdata['redeem']) ? $allvoucherdata['redeem'] : '';
//start date
$start_date = isset($allvoucherdata['start_date']) ? $allvoucherdata['start_date'] : '';
//expiry data
$exp_date = isset($allvoucherdata['exp_date']) ? $allvoucherdata['exp_date'] : '';
//vou order date
$orderdate = get_the_time('Y-m-d', $orderid);
if (!empty($orderdate)) {
$orderdate = $model->woo_vou_get_date_format($orderdate);
}
//vou logo
$voulogo = isset($allvoucherdata['vendor_logo']) ? $allvoucherdata['vendor_logo'] : '';
$voulogo = isset($voulogo['src']) && !empty($voulogo['src']) ? $voulogo['src'] : '';
//vendor logo
$voulogohtml = '';
if (!empty($voulogo)) {
$voulogohtml = '<img src="' . $voulogo . '" alt="" />';
}
//site logo
$vousitelogohtml = '';
$vou_site_url = get_option('vou_site_logo');
if (!empty($vou_site_url)) {
$vousitelogohtml = '<img src="' . $vou_site_url . '" alt="" />';
//.........这里部分代码省略.........
public function handle_download($query)
{
//gets the global query var object
global $wp_query;
if ($wp_query->get('bx-download')) {
$item_id = $wp_query->get('bx-item-id');
$download_id = $wp_query->get('bx-download-id');
$bx_links = wc_get_order_item_meta($item_id, '_bx_downloadlinks');
if (is_array($bx_links)) {
if ($download_id === wc_get_order_item_meta($item_id, '_bx_epub_link')) {
$link = $bx_links['epub'];
} elseif ($download_id === wc_get_order_item_meta($item_id, '_bx_mobi_link')) {
$link = $bx_links['mobi'];
}
header('Location: ' . $link);
exit;
} else {
//gets the front page id set in options
$page_id = get_option('woocommerce_download_processing_page_id');
if (false === $page_id) {
echo '<h1>' . __('Your download is not ready yet', 'woocommerce_booxtream') . '</h1>';
echo '<p>' . __('We are currently processing your download, please try again in a few seconds', 'woocommerce_booxtream') . '</p>';
exit;
}
if (!$query->is_main_query()) {
return;
}
$query->set('post_type', 'page');
$query->set('p', $page_id);
//we remove the actions hooked on the '__after_loop' (post navigation)
remove_all_actions('__after_loop');
return $query;
}
}
}
/**
* Create or update a line item
*
* @since 2.2
* @param \WC_Order $order
* @param array $item line item data
* @param string $action 'create' to add line item or 'update' to update it
* @throws WC_API_Exception invalid data, server error
*/
protected function set_line_item($order, $item, $action)
{
$creating = 'create' === $action;
// product is always required
if (!isset($item['product_id'])) {
throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID is required', 'woocommerce'), 400);
}
// when updating, ensure product ID provided matches
if ('update' === $action) {
$item_product_id = wc_get_order_item_meta($item['id'], '_product_id');
$item_variation_id = wc_get_order_item_meta($item['id'], '_variation_id');
if ($item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id) {
throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID provided does not match this line item', 'woocommerce'), 400);
}
}
$product = wc_get_product($item['product_id']);
// must be a valid WC_Product
if (!is_object($product)) {
throw new WC_API_Exception('woocommerce_api_invalid_product', __('Product is invalid', 'woocommerce'), 400);
}
// quantity must be positive float
if (isset($item['quantity']) && floatval($item['quantity']) <= 0) {
throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity must be a positive float', 'woocommerce'), 400);
}
// quantity is required when creating
if ($creating && !isset($item['quantity'])) {
throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity is required', 'woocommerce'), 400);
}
$item_args = array();
// quantity
if (isset($item['quantity'])) {
$item_args['qty'] = $item['quantity'];
}
// variations must each have a key & value
if (isset($item['variations']) && is_array($item['variations'])) {
foreach ($item['variations'] as $key => $value) {
if (!$key || !$value) {
throw new WC_API_Exception('woocommerce_api_invalid_product_variation', __('The product variation is invalid', 'woocommerce'), 400);
}
}
$item_args['variation'] = $item['variations'];
}
// total
if (isset($item['total'])) {
$item_args['totals']['total'] = floatval($item['total']);
}
// total tax
if (isset($item['total_tax'])) {
$item_args['totals']['tax'] = floatval($item['total_tax']);
}
// subtotal
if (isset($item['subtotal'])) {
$item_args['totals']['subtotal'] = floatval($item['subtotal']);
}
// subtotal tax
if (isset($item['subtotal_tax'])) {
$item_args['totals']['subtotal_tax'] = floatval($item['subtotal_tax']);
}
if ($creating) {
$item_id = $order->add_product($product, $item_args['qty'], $item_args);
if (!$item_id) {
throw new WC_API_Exception('woocommerce_cannot_create_line_item', __('Cannot create line item, try again', 'woocommerce'), 500);
}
} else {
$item_id = $order->update_product($item['id'], $product, $item_args);
if (!$item_id) {
throw new WC_API_Exception('woocommerce_cannot_update_line_item', __('Cannot update line item, try again', 'woocommerce'), 500);
}
}
}
public static function compile_ticket_info_images($current, $oiid, $order_id)
{
// do not process this unless the ticket information has been loaded
if (!is_object($current) || is_wp_error($current)) {
return $current;
}
// create the list of pairs to calculate
$pairs = array('image_id_left' => self::$options->{'qsot-ticket-image-shown'}, 'image_id_right' => self::$options->{'qsot-ticket-image-shown-right'});
// calculate each pair
foreach ($pairs as $key => $setting) {
switch ($setting) {
default:
case 'event':
if (isset($current->event, $current->event->image_id)) {
$current->{$key} = $current->event->image_id;
}
break;
case 'product':
$product = wc_get_product(wc_get_order_item_meta($oiid, '_product_id', true));
if (is_object($product)) {
$current->{$key} = get_post_thumbnail_id($product->id);
}
break;
case 'venue':
if (isset($current->venue, $current->venue->image_id)) {
$current->{$key} = $current->venue->image_id;
}
break;
case 'none':
$current->{$key} = 0;
break;
}
}
return $current;
}
请发表评论