/** * export_orders. * * @version 2.5.9 * @since 2.4.8 * @todo (maybe) metainfo as separate column */ function export_orders($fields_helper) { // Standard Fields $all_fields = $fields_helper->get_order_export_fields(); $fields_ids = get_option('wcj_export_orders_fields', $fields_helper->get_order_export_default_fields_ids()); $titles = array(); foreach ($fields_ids as $field_id) { $titles[] = $all_fields[$field_id]; } // Additional Fields $total_number = apply_filters('booster_get_option', 1, get_option('wcj_export_orders_fields_additional_total_number', 1)); for ($i = 1; $i <= $total_number; $i++) { if ('yes' === get_option('wcj_export_orders_fields_additional_enabled_' . $i, 'no')) { $titles[] = get_option('wcj_export_orders_fields_additional_title_' . $i, ''); } } $data = array(); $data[] = $titles; $offset = 0; $block_size = 1024; while (true) { $args_orders = array('post_type' => 'shop_order', 'post_status' => 'any', 'posts_per_page' => $block_size, 'orderby' => 'date', 'order' => 'DESC', 'offset' => $offset, 'fields' => 'ids'); $loop_orders = new WP_Query($args_orders); if (!$loop_orders->have_posts()) { break; } foreach ($loop_orders->posts as $order_id) { $order = wc_get_order($order_id); if (isset($_POST['wcj_filter_by_order_billing_country']) && '' != $_POST['wcj_filter_by_order_billing_country']) { if ($order->billing_country != $_POST['wcj_filter_by_order_billing_country']) { continue; } } $filter_by_product_title = true; if (isset($_POST['wcj_filter_by_product_title']) && '' != $_POST['wcj_filter_by_product_title']) { $filter_by_product_title = false; } $items = array(); $items_product_input_fields = array(); foreach ($order->get_items() as $item_id => $item) { if (in_array('order-items', $fields_ids)) { $meta_info = 0 != $item['variation_id'] ? wcj_get_order_item_meta_info($item_id, $item, $order, true) : ''; if ('' != $meta_info) { $meta_info = ' [' . $meta_info . ']'; } $items[] = $item['name'] . $meta_info; } if (in_array('order-items-product-input-fields', $fields_ids)) { $item_product_input_fields = wcj_get_product_input_fields($item); if ('' != $item_product_input_fields) { $items_product_input_fields[] = $item_product_input_fields; } } if (!$filter_by_product_title) { // if ( $item['name'] === $_POST['wcj_filter_by_product_title'] ) { if (false !== strpos($item['name'], $_POST['wcj_filter_by_product_title'])) { $filter_by_product_title = true; } } } $items = implode(' / ', $items); $items_product_input_fields = implode(' / ', $items_product_input_fields); if (!$filter_by_product_title) { continue; } $row = $this->get_export_orders_row($fields_ids, $order_id, $order, $items, $items_product_input_fields, null, null); // Additional Fields $total_number = apply_filters('booster_get_option', 1, get_option('wcj_export_orders_fields_additional_total_number', 1)); for ($i = 1; $i <= $total_number; $i++) { if ('yes' === get_option('wcj_export_orders_fields_additional_enabled_' . $i, 'no')) { if ('' != ($additional_field_value = get_option('wcj_export_orders_fields_additional_value_' . $i, ''))) { if ('meta' === get_option('wcj_export_orders_fields_additional_type_' . $i, 'meta')) { $row[] = get_post_meta($order_id, $additional_field_value, true); } else { global $post; $post = get_post($order_id); setup_postdata($post); $row[] = do_shortcode($additional_field_value); wp_reset_postdata(); } } else { $row[] = ''; } } } $data[] = $row; } $offset += $block_size; } return $data; }
/** * wcj_order_items_table. * * @version 2.5.9 */ function wcj_order_items_table($atts, $content = '') { $html = ''; $the_order = $this->the_order; // Get columns $columns = explode('|', $atts['columns']); if (empty($columns)) { return ''; } $columns_total_number = count($columns); // Check all possible args $columns_titles = '' == $atts['columns_titles'] ? array() : explode('|', $atts['columns_titles']); $columns_styles = '' == $atts['columns_styles'] ? array() : explode('|', $atts['columns_styles']); /* if ( $columns_total_number !== count( $columns_titles ) || $columns_total_number !== count( $columns_styles ) ) { return __( 'Please recheck that there is the same number of columns in "columns", "columns_titles" and "columns_styles" attributes.', 'woocommerce-jetpack' ); } */ // The Items $the_items = $the_order->get_items(); // Shipping as item if ('' != $atts['shipping_as_item'] && $the_order->get_total_shipping() > 0) { $name = str_replace('%shipping_method_name%', $the_order->get_shipping_method(), $atts['shipping_as_item']); $total_shipping_tax_excl = $the_order->get_total_shipping(); $shipping_tax = $the_order->get_shipping_tax(); $the_items = $this->add_item($the_items, array('name' => $name, 'qty' => 1, 'line_subtotal' => $total_shipping_tax_excl, 'line_total' => $total_shipping_tax_excl, 'line_tax' => $shipping_tax, 'line_subtotal_tax' => $shipping_tax)); } // Discount as item if ('' != $atts['discount_as_item'] && $the_order->get_total_discount(true) > 0) { $name = $atts['discount_as_item']; $total_discount_tax_excl = $the_order->get_total_discount(true); $discount_tax = $the_order->get_total_discount(false) - $total_discount_tax_excl; /* if ( false != ( $the_tax = $this->wcj_order_get_cart_discount_tax() ) ) { $total_discount_tax_excl -= $the_tax; $discount_tax += $the_tax; } */ $total_discount_tax_excl *= -1; $discount_tax *= -1; $the_items = $this->add_item($the_items, array('name' => $name, 'qty' => 1, 'line_subtotal' => $total_discount_tax_excl, 'line_total' => $total_discount_tax_excl, 'line_tax' => $discount_tax, 'line_subtotal_tax' => $discount_tax)); } // Starting data[] by adding columns titles $data = array(); foreach ($columns_titles as $column_title) { $data[0][] = $column_title; } // Items to data[] $item_counter = 0; foreach ($the_items as $item_id => $item) { $item['is_custom'] = isset($item['is_custom']) ? true : false; $the_product = true === $item['is_custom'] ? null : $the_order->get_product_from_item($item); $item_counter++; // Columns foreach ($columns as $column) { if (false !== ($pos = strpos($column, '='))) { $column_param = substr($column, $pos + 1); $column = substr($column, 0, $pos); } switch ($column) { case 'item_debug': case 'debug': $data[$item_counter][] = print_r($item, true); break; case 'item_regular_price': case 'product_regular_price': $data[$item_counter][] = is_object($the_product) ? $this->wcj_price_shortcode($the_product->get_regular_price(), $atts) : ''; break; case 'item_sale_price': case 'product_sale_price': $data[$item_counter][] = is_object($the_product) ? $this->wcj_price_shortcode($the_product->get_sale_price(), $atts) : ''; break; case 'item_tax_class': case 'tax_class': $data[$item_counter][] = isset($item['tax_class']) ? $this->get_tax_class_name($item['tax_class']) : ''; break; case 'item_number': $data[$item_counter][] = $item_counter; break; case 'item_meta': $data[$item_counter][] = wcj_get_order_item_meta_info($item_id, null, $this->the_order, false, $the_product); break; case 'item_name': case 'product_name': // "product_" because of possible variation if (true === $item['is_custom']) { $data[$item_counter][] = $item['name']; } else { $the_item_title = $item['name']; // Variation (if needed) if (0 != $item['variation_id'] && !in_array('item_variation', $columns)) { $the_item_title .= '<div style="' . $atts['style_item_name_variation'] . '">'; if ('yes' === $atts['variation_as_metadata']) { $the_item_title .= wcj_get_order_item_meta_info($item_id, null, $this->the_order, true, $the_product); } elseif (is_object($the_product) && $the_product->is_type('variation')) { $the_item_title .= str_replace('pa_', '', urldecode($the_product->get_formatted_variation_attributes(true))); // todo - do we need pa_ replacement? } $the_item_title .= '</div>'; } // "WooCommerce TM Extra Product Options" plugin options // todo - this will show options prices in shop's default currency only (must use 'price_per_currency' to show prices in order's currency). if (isset($item['tmcartepo_data'])) { $options = unserialize($item['tmcartepo_data']); $options_prices = array(); // $order_currency = $the_order->get_order_currency(); foreach ($options as $option) { /* if ( isset( $option['price_per_currency'][ $order_currency ] ) ) { $options_prices[] = $this->wcj_price_shortcode( $option['price_per_currency'][ $order_currency ], $atts ); } */ $option_info = ''; if (isset($option['name']) && '' != $option['name']) { $option_info .= $option['name'] . ': '; } if (isset($option['value']) && '' != $option['value']) { $option_info .= $option['value']; } if (isset($option['price']) && 'yes' === $atts['wc_extra_product_options_show_price']) { // todo - wc_extra_product_options_show_price is temporary, until price_per_currency issue is solved $option_info .= $option['price'] > 0 ? ' +' . wc_price($option['price']) : ' ' . wc_price($option['price']); } if ('' != $option_info) { $options_prices[] = $option_info; } } $the_item_title .= '<div style="' . $atts['style_item_name_variation'] . '">' . implode('<br>', $options_prices) . '</div>'; } $data[$item_counter][] = $the_item_title; } break; case 'item_product_input_fields': $data[$item_counter][] = wcj_get_product_input_fields($item); break; case 'item_key': if (isset($column_param) && '' != $column_param && isset($item[$column_param])) { $maybe_unserialized_value = maybe_unserialize($item[$column_param]); if (is_array($maybe_unserialized_value)) { $data[$item_counter][] = isset($maybe_unserialized_value['name']) ? $maybe_unserialized_value['name'] : ''; } else { $data[$item_counter][] = $maybe_unserialized_value; } } else { $data[$item_counter][] = ''; } break; case 'item_attribute': case 'product_attribute': if (isset($column_param) && '' != $column_param && is_object($the_product)) { $data[$item_counter][] = $the_product->get_attribute($column_param); } else { $data[$item_counter][] = ''; } break; case 'item_excerpt': case 'product_excerpt': if (true === $item['is_custom']) { $data[$item_counter][] = ''; } else { global $post; $post = get_post($item['product_id']); setup_postdata($post); $the_excerpt = get_the_excerpt(); wp_reset_postdata(); $data[$item_counter][] = $the_excerpt; } break; case 'item_short_description': case 'product_short_description': $data[$item_counter][] = true === $item['is_custom'] ? '' : $this->the_product->post->post_excerpt; break; case 'item_variation': case 'product_variation': if (0 != $item['variation_id']) { if ('yes' === $atts['variation_as_metadata']) { $data[$item_counter][] = wcj_get_order_item_meta_info($item_id, null, $this->the_order, true, $the_product); } elseif (is_object($the_product) && $the_product->is_type('variation')) { $data[$item_counter][] = str_replace('pa_', '', urldecode($the_product->get_formatted_variation_attributes(true))); // todo - do we need pa_ replacement? } } else { $data[$item_counter][] = ''; } break; case 'item_thumbnail': case 'product_thumbnail': // $data[ $item_counter ][] = $the_product->get_image(); $image_id = true === $item['is_custom'] || !is_object($the_product) ? 0 : $the_product->get_image_id(); $image_src = 0 != $image_id ? wp_get_attachment_image_src($image_id) : wc_placeholder_img_src(); if (is_array($image_src)) { $image_src = $image_src[0]; } $maybe_width = 0 != $atts['product_image_width'] ? ' width="' . $atts['product_image_width'] . '"' : ''; $maybe_height = 0 != $atts['product_image_height'] ? ' height="' . $atts['product_image_height'] . '"' : ''; $data[$item_counter][] = '<img src="' . $image_src . '"' . $maybe_width . $maybe_height . '>'; break; case 'item_sku': case 'product_sku': $data[$item_counter][] = true === $item['is_custom'] || !is_object($the_product) ? '' : $the_product->get_sku(); break; case 'item_quantity': $data[$item_counter][] = $atts['quantity_prefix'] . $item['qty']; break; case 'item_total_tax_excl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_item_total($item, false, true), $atts); break; case 'item_total_tax_incl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_item_total($item, true, true), $atts); break; case 'item_subtotal_tax_excl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_item_subtotal($item, false, true), $atts); break; case 'item_subtotal_tax_incl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_item_subtotal($item, true, true), $atts); break; case 'item_tax': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_item_tax($item, true), $atts); break; case 'line_total_tax_excl': $line_total_tax_excl = $the_order->get_line_total($item, false, true); $line_total_tax_excl = apply_filters('wcj_line_total_tax_excl', $line_total_tax_excl, $the_order); $data[$item_counter][] = $this->wcj_price_shortcode($line_total_tax_excl, $atts); break; case 'line_total_tax_incl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_line_total($item, true, true), $atts); break; case 'line_subtotal_tax_excl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_line_subtotal($item, false, true), $atts); break; case 'line_subtotal_tax_incl': $data[$item_counter][] = $this->wcj_price_shortcode($the_order->get_line_subtotal($item, true, true), $atts); break; case 'line_tax': $line_tax = $the_order->get_line_tax($item); $line_tax = apply_filters('wcj_line_tax', $line_tax, $the_order); $data[$item_counter][] = $this->wcj_price_shortcode($line_tax, $atts); break; case 'line_subtax': $line_subtax = $the_order->get_line_subtotal($item, true, false) - $the_order->get_line_subtotal($item, false, false); $data[$item_counter][] = $this->wcj_price_shortcode($line_subtax, $atts); break; case 'item_tax_percent': case 'line_tax_percent': $item_total = $the_order->get_item_total($item, false, false); $item_tax_percent = 0 != $item_total ? $the_order->get_item_tax($item, false) / $item_total * 100 : 0; $item_tax_percent = apply_filters('wcj_line_tax_percent', $item_tax_percent, $the_order); $data[$item_counter][] = sprintf($atts['tax_percent_format'], $item_tax_percent); /* $tax_labels = array(); foreach ( $the_order->get_taxes() as $the_tax ) { $tax_labels[] = $the_tax['label']; } $data[ $item_counter ][] = implode( ', ', $tax_labels ); */ break; /* case 'line_tax_percent': $line_total = $the_order->get_line_total( $item, false, true ); $line_tax_percent = ( 0 != $line_total ) ? $the_order->get_line_tax( $item ) / $line_total * 100 : 0; $line_tax_percent = apply_filters( 'wcj_line_tax_percent', $line_tax_percent, $the_order ); $data[ $item_counter ][] = sprintf( $atts['tax_percent_format'], $line_tax_percent ); break; */ /* case 'line_tax_percent': $line_total = $the_order->get_line_total( $item, false, true ); $line_tax_percent = ( 0 != $line_total ) ? $the_order->get_line_tax( $item ) / $line_total * 100 : 0; $line_tax_percent = apply_filters( 'wcj_line_tax_percent', $line_tax_percent, $the_order ); $data[ $item_counter ][] = sprintf( $atts['tax_percent_format'], $line_tax_percent ); break; */ case 'item_weight': case 'product_weight': $data[$item_counter][] = true === $item['is_custom'] || !is_object($the_product) ? '' : $the_product->get_weight(); break; case 'item_width': case 'product_width': $data[$item_counter][] = true === $item['is_custom'] || !is_object($the_product) ? '' : $the_product->get_width(); break; case 'item_height': case 'product_height': $data[$item_counter][] = true === $item['is_custom'] || !is_object($the_product) ? '' : $the_product->get_height(); break; case 'item_length': case 'product_length': $data[$item_counter][] = true === $item['is_custom'] || !is_object($the_product) ? '' : $the_product->get_length(); break; default: $data[$item_counter][] = ''; // $column; } } } $html = wcj_get_table_html($data, array('table_class' => $atts['table_class'], 'table_heading_type' => 'horizontal', 'columns_classes' => array(), 'columns_styles' => $columns_styles)); return $html; }