function woo_ce_export_dataset( $export_type = null, &$output = null ) { global $export; $separator = $export->delimiter; $line_ending = woo_ce_get_line_ending(); $export->columns = array(); $export->total_rows = 0; $export->total_columns = 0; $troubleshooting_url = 'http://www.visser.com.au/documentation/store-exporter-deluxe/usage/'; set_transient( WOO_CD_PREFIX . '_running', time(), woo_ce_get_option( 'timeout', MINUTE_IN_SECONDS ) ); // Load up the fatal error notice if we 500 Internal Server Error (memory), hit a server timeout or encounter a fatal PHP error add_action( 'shutdown', 'woo_ce_fatal_error' ); // Drop in our content filters here add_filter( 'sanitize_key', 'woo_ce_sanitize_key' ); switch( $export_type ) { // Products case 'product': $fields = woo_ce_get_product_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_product_field( $key ); } if( $export->gallery_unique ) { $export->fields = woo_ce_unique_product_gallery_fields( $export->fields ); $export->columns = woo_ce_unique_product_gallery_columns( $export->columns, $export->fields ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $products = woo_ce_get_products( $export->args ) ) { $export->total_rows = count( $products ); // XML export if( $export->export_format == 'xml' ) { if( !empty( $export->fields ) ) { foreach( $products as $product ) { $child = $output->addChild( apply_filters( 'woo_ce_export_xml_product_node', sanitize_key( $export_type ) ) ); $product = woo_ce_get_product_data( $product, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $product->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $product->$field, $export_type, $field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( woo_ce_sanitize_xml_string( $product->$field ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $product->$field ) ) ); } } } } } } else if( $export->export_format == 'rss' ) { // RSS export if( !empty( $export->fields ) ) { foreach( $products as $product ) { $child = $output->addChild( 'item' ); $product = woo_ce_get_product_data( $product, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $field ) { if( isset( $product->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $product->$field ) ) $child->addChild( sanitize_key( $field ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $product->$field ) ) ); else $child->addChild( sanitize_key( $field ), esc_html( woo_ce_sanitize_xml_string( $product->$field ) ) ); } } } } } } else { // PHPExcel export foreach( $products as $key => $product ) { $products[$key] = woo_ce_get_product_data( $product, $export->args, array_keys( $export->fields ) ); } $output = $products; } unset( $products, $product ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Categories case 'category': $fields = woo_ce_get_category_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_category_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); $category_args = array( 'orderby' => ( isset( $export->args['category_orderby'] ) ? $export->args['category_orderby'] : 'ID' ), 'order' => ( isset( $export->args['category_order'] ) ? $export->args['category_order'] : 'ASC' ), ); if( $categories = woo_ce_get_product_categories( $category_args ) ) { $export->total_rows = count( $categories ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $categories as $category ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_category_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $category->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $category->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $category->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $category->$field ) ) ); } } } } } } else { // PHPExcel export $output = $categories; } unset( $categories, $category ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Tags case 'tag': $fields = woo_ce_get_tag_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_tag_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); $tag_args = array( 'orderby' => ( isset( $export->args['tag_orderby'] ) ? $export->args['tag_orderby'] : 'ID' ), 'order' => ( isset( $export->args['tag_order'] ) ? $export->args['tag_order'] : 'ASC' ), ); if( $tags = woo_ce_get_product_tags( $tag_args ) ) { $export->total_rows = count( $tags ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $tags as $tag ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_tag_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $tag->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $tag->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $tag->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $tag->$field ) ) ); } } } } } } else { // PHPExcel export $output = $tags; } unset( $tags, $tag ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Brands case 'brand': $fields = woo_ce_get_brand_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_brand_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); $brand_args = array( 'orderby' => ( isset( $export->args['brand_orderby'] ) ? $export->args['brand_orderby'] : 'ID' ), 'order' => ( isset( $export->args['brand_order'] ) ? $export->args['brand_order'] : 'ASC' ), ); if( $brands = woo_ce_get_product_brands( $brand_args ) ) { $export->total_rows = count( $brands ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $brands as $brand ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_brand_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $brand->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $brand->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $brand->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $brand->$field ) ) ); } } } } } } else { // PHPExcel export $output = $brands; } unset( $brands, $brand ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Orders case 'order': $fields = woo_ce_get_order_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { // Do not apply Field Editor changes to the unique Order Items Formatting rule if( $export->args['order_items'] == 'unique' ) remove_filter( 'woo_ce_order_fields', 'woo_ce_override_order_field_labels', 11 ); foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_order_field( $key ); // Do not apply Field Editor changes to the unique Order Items Formatting rule if( $export->args['order_items'] == 'unique' ) add_filter( 'woo_ce_order_fields', 'woo_ce_override_order_field_labels', 11 ); } if( $export->args['order_items'] == 'unique' ) { $export->fields = woo_ce_unique_order_item_fields( $export->fields ); $export->columns = woo_ce_unique_order_item_columns( $export->columns, $export->fields ); } $export->data_memory_start = woo_ce_current_memory_usage(); if( $orders = woo_ce_get_orders( 'order', $export->args ) ) { $export->total_columns = $size = count( $export->columns ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $orders as $order ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_order_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); $child->addAttribute( 'id', $order ); $order = woo_ce_get_order_data( $order, 'order', $export->args, array_keys( $export->fields ) ); if( in_array( $export->args['order_items'], array( 'combined', 'unique' ) ) ) { // Order items formatting: SPECK-IPHONE|INCASE-NANO|- foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $order->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $order->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) ); } } } } else if( $export->args['order_items'] == 'individual' ) { // Order items formatting: SPECK-IPHONE<br />INCASE-NANO<br />- if( !empty( $order->order_items ) ) { $order->order_items_product_id = ''; $order->order_items_variation_id = ''; $order->order_items_sku = ''; $order->order_items_name = ''; $order->order_items_variation = ''; $order->order_items_description = ''; $order->order_items_excerpt = ''; $order->order_items_tax_class = ''; $order->order_items_quantity = ''; $order->order_items_total = ''; $order->order_items_subtotal = ''; $order->order_items_rrp = ''; $order->order_items_stock = ''; $order->order_items_tax = ''; $order->order_items_tax_subtotal = ''; $order->order_items_type = ''; $order->order_items_category = ''; $order->order_items_tag = ''; $order->order_items_total_sales = ''; $order->order_items_weight = ''; $order->order_items_total_weight = ''; foreach( $order->order_items as $order_item ) { // Add Order Item weight to Shipping Weight if( $order_item->total_weight != '' ) $order->shipping_weight = $order->shipping_weight + $order_item->total_weight; $order->order_items_product_id = $order_item->product_id; $order->order_items_variation_id = $order_item->variation_id; if( empty( $order_item->sku ) ) $order_item->sku = ''; $order->order_items_sku = $order_item->sku; $order->order_items_name = $order_item->name; $order->order_items_variation = $order_item->variation; $order->order_items_description = woo_ce_format_description_excerpt( $order_item->description ); $order->order_items_excerpt = woo_ce_format_description_excerpt( $order_item->excerpt ); $order->order_items_tax_class = $order_item->tax_class; $order->total_quantity += $order_item->quantity; $order->order_items_quantity = $order_item->quantity; $order->order_items_total = $order_item->total; $order->order_items_subtotal = $order_item->subtotal; $order->order_items_rrp = $order_item->rrp; $order->order_items_stock = $order_item->stock; $order->order_items_tax = $order_item->tax; $order->order_items_tax_subtotal = $order_item->tax_subtotal; $order->order_items_type = $order_item->type; $order->order_items_category = $order_item->category; $order->order_items_tag = $order_item->tag; $order->order_items_total_sales = $order_item->total_sales; $order->order_items_weight = $order_item->weight; $order->order_items_total_weight = $order_item->total_weight; $order = apply_filters( 'woo_ce_order_items_individual', $order, $order_item ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $order->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $order->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) ); } } } } unset( $order->order_items ); } } } } } else { // PHPExcel export if( $export->args['order_items'] == 'individual' ) $output = array(); foreach( $orders as $order ) { if( in_array( $export->args['order_items'], array( 'combined', 'unique' ) ) ) { // Order items formatting: SPECK-IPHONE|INCASE-NANO|- $output[] = woo_ce_get_order_data( $order, 'order', $export->args, array_keys( $export->fields ) ); } else if( $export->args['order_items'] == 'individual' ) { // Order items formatting: SPECK-IPHONE<br />INCASE-NANO<br />- $order = woo_ce_get_order_data( $order, 'order', $export->args, array_keys( $export->fields ) ); if( !empty( $order->order_items ) ) { foreach( $order->order_items as $order_item ) { // Add Order Item weight to Shipping Weight if( $order_item->total_weight != '' ) $order->shipping_weight = $order->shipping_weight + $order_item->total_weight; $order->order_items_product_id = $order_item->product_id; $order->order_items_variation_id = $order_item->variation_id; if( empty( $order_item->sku ) ) $order_item->sku = ''; $order->order_items_sku = $order_item->sku; $order->order_items_name = $order_item->name; $order->order_items_variation = $order_item->variation; $order->order_items_description = $order_item->description; $order->order_items_excerpt = $order_item->excerpt; $order->order_items_tax_class = $order_item->tax_class; $order->total_quantity += $order_item->quantity; $order->order_items_quantity = $order_item->quantity; $order->order_items_total = $order_item->total; $order->order_items_subtotal = $order_item->subtotal; $order->order_items_rrp = $order_item->rrp; $order->order_items_stock = $order_item->stock; $order->order_items_tax = $order_item->tax; $order->order_items_tax_subtotal = $order_item->tax_subtotal; $order->order_items_type = $order_item->type; $order->order_items_category = $order_item->category; $order->order_items_tag = $order_item->tag; $order->order_items_total_sales = $order_item->total_sales; $order->order_items_weight = $order_item->weight; $order->order_items_total_weight = $order_item->total_weight; $order = apply_filters( 'woo_ce_order_items_individual', $order, $order_item ); // This fixes the Order Items for this Order Items Formatting rule $output[] = (object)(array)$order; } } } } } unset( $orders, $order ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Customers case 'customer': $fields = woo_ce_get_customer_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_customer_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $customers = woo_ce_get_orders( 'customer', $export->args ) ) { $export->total_rows = count( $customers ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $customers as $customer ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_customer_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $customer->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $customer->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $customer->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $customer->$field ) ) ); } } } } } } else { // PHPExcel export $output = $customers; } unset( $customers, $customer ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Users case 'user': $fields = woo_ce_get_user_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_user_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $users = woo_ce_get_users( $export->args ) ) { // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { $export->total_rows = count( $users ); if( !empty( $export->fields ) ) { foreach( $users as $user ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_user_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); $user = woo_ce_get_user_data( $user, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $user->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $user->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $user->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $user->$field ) ) ); } } } } } } else { // PHPExcel export foreach( $users as $key => $user ) $users[$key] = woo_ce_get_user_data( $user, $export->args, array_keys( $export->fields ) ); $output = $users; } unset( $users, $user ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Coupons case 'coupon': $fields = woo_ce_get_coupon_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_coupon_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $coupons = woo_ce_get_coupons( $export->args ) ) { // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { $export->total_rows = count( $coupons ); if( !empty( $export->fields ) ) { foreach( $coupons as $coupon ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_coupon_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); $coupon = woo_ce_get_coupon_data( $coupon, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $coupon->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $coupon->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $coupon->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $coupon->$field ) ) ); } } } } } } else { // PHPExcel export foreach( $coupons as $key => $coupon ) { $coupons[$key] = woo_ce_get_coupon_data( $coupon, $export->args, array_keys( $export->fields ) ); } $output = $coupons; } unset( $coupons, $coupon ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Subscriptions case 'subscription': $fields = woo_ce_get_subscription_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_subscription_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $subscriptions = woo_ce_get_subscriptions( $export->args ) ) { $export->total_rows = count( $subscriptions ); // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { if( !empty( $export->fields ) ) { foreach( $subscriptions as $subscription ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_subscription_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $subscription->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $subscription->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $subscription->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $subscription->$field ) ) ); } } } } } } else { // PHPExcel export $output = $subscriptions; } unset( $subscriptions, $subscription ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Product Vendors case 'product_vendor': $fields = woo_ce_get_product_vendor_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_product_vendor_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $product_vendors = woo_ce_get_product_vendors( $export->args ) ) { // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { $export->total_rows = count( $product_vendors ); if( !empty( $export->fields ) ) { foreach( $product_vendors as $product_vendor ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_product_vendor_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); $product_vendor = woo_ce_get_product_vendor_data( $product_vendor, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $product_vendor->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $product_vendor->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $product_vendor->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $product_vendor->$field ) ) ); } } } } } } else { // PHPExcel export foreach( $product_vendors as $key => $product_vendor ) { $product_vendors[$key] = woo_ce_get_product_vendor_data( $product_vendor, $export->args, array_keys( $export->fields ) ); } $output = $product_vendors; } unset( $product_vendors, $product_vendor ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Commissions case 'commission': $fields = woo_ce_get_commission_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_commission_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $commissions = woo_ce_get_commissions( $export->args ) ) { // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { $export->total_rows = count( $commissions ); if( !empty( $export->fields ) ) { foreach( $commissions as $commission ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_commission_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); $commission = woo_ce_get_commission_data( $commission, $export->args, array_keys( $export->fields ) ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $commission->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $commission->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $commission->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $commission->$field ) ) ); } } } } } } else { // PHPExcel export foreach( $commissions as $key => $commission ) { $commissions[$key] = woo_ce_get_commission_data( $commission, $export->args, array_keys( $export->fields ) ); } $output = $commissions; } unset( $commissions, $commission ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; // Shipping Classes case 'shipping_class': $fields = woo_ce_get_shipping_class_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_shipping_class_field( $key ); } $export->total_columns = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $shipping_classes = woo_ce_get_shipping_classes( $export->args ) ) { // XML, RSS export if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { $export->total_rows = count( $shipping_classes ); if( !empty( $export->fields ) ) { foreach( $shipping_classes as $shipping_class ) { if( $export->export_format == 'xml' ) $child = $output->addChild( apply_filters( 'woo_ce_export_xml_shipping_class_node', sanitize_key( $export_type ) ) ); else if( $export->export_format == 'rss' ) $child = $output->addChild( 'item' ); foreach( array_keys( $export->fields ) as $key => $field ) { if( isset( $shipping_class->$field ) ) { if( !is_array( $field ) ) { if( woo_ce_is_xml_cdata( $shipping_class->$field ) ) $child->addChild( sanitize_key( $export->columns[$key] ) )->addCData( esc_html( woo_ce_sanitize_xml_string( $shipping_class->$field ) ) ); else $child->addChild( sanitize_key( $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $shipping_class->$field ) ) ); } } } } } } else { // PHPExcel export $output = $shipping_classes; } unset( $shipping_classes, $shipping_class ); } $export->data_memory_end = woo_ce_current_memory_usage(); break; /* // Attributes case 'attribute': $fields = woo_ce_get_attribute_fields( 'summary' ); if( $export->fields = array_intersect_assoc( (array)$export->fields, $fields ) ) { foreach( $export->fields as $key => $field ) $export->columns[] = woo_ce_get_attribute_field( $key ); } $export->total_columns = $size = count( $export->columns ); $export->data_memory_start = woo_ce_current_memory_usage(); if( $attributes = woo_ce_get_attributes( $export->args ) ) { $export->total_rows = count( $attributes ); // Generate the export headers if( $export->header_formatting && in_array( $export->export_format, array( 'csv', 'xls' ) ) ) { for( $i = 0; $i < $size; $i++ ) { if( $i == ( $size - 1 ) ) $output .= woo_ce_escape_csv_value( $export->columns[$i], $export->delimiter, $export->escape_formatting ) . $line_ending; else $output .= woo_ce_escape_csv_value( $export->columns[$i], $export->delimiter, $export->escape_formatting ) . $separator; } } if( !empty( $export->fields ) ) { foreach( $atributes as $attribute ) { if( $export->export_format == 'xml' ) $child = $output->addChild( $export->type, 0, -1 ); } } } $export->data_memory_end = woo_ce_current_memory_usage(); unset( $export->fields ); break; */ } // Remove our content filters here to play nice with other Plugins remove_filter( 'sanitize_key', 'woo_ce_sanitize_key' ); // Remove our fatal error notice so not to conflict with the CRON or scheduled export engine remove_action( 'shutdown', 'woo_ce_fatal_error' ); // Export completed successfully delete_transient( WOO_CD_PREFIX . '_running' ); // Check if we're using PHPExcel or generic export engine if( WOO_CD_DEBUG || in_array( $export->export_format, array( 'xml', 'rss' ) ) ) { // Check that the export file is populated, export columns have been assigned and rows counted if( !empty( $output ) && $export->total_rows && $export->total_columns ) { if( WOO_CD_DEBUG && !in_array( $export->export_format, array( 'csv', 'xls', 'xlsx' ) ) && ( !$export->cron && !$export->scheduled_export ) ) { if( in_array( $export->export_format, array( 'xml', 'rss' ) ) ) $output = woo_ce_format_xml( $output ); $response = set_transient( WOO_CD_PREFIX . '_debug_log', base64_encode( $output ), woo_ce_get_option( 'timeout', MINUTE_IN_SECONDS ) ); if( $response !== true ) { $message = __( 'The export contents were too large to store in a single WordPress transient, use the Volume offset / Limit volume options to reduce the size of your export and try again.', 'woo_ce' ) . ' (<a href="' . $troubleshooting_url . '" target="_blank">' . __( 'Need help?', 'woo_ce' ) . '</a>)'; if( function_exists( 'woo_cd_admin_notice' ) ) woo_cd_admin_notice( $message, 'error' ); else error_log( sprintf( '[store-exporter-deluxe] woo_ce_export_dataset() - %s', $message ) ); return; } else { return true; } } else { return $output; } } } else { return $output; } }
function woo_ce_get_orders( $export_type = 'order', $args = array() ) { global $export; $limit_volume = -1; $offset = 0; if( $args ) { $order_ids = ( isset( $args['order_ids'] ) ? $args['order_ids'] : false ); $payment = ( isset( $args['order_payment'] ) ? $args['order_payment'] : false ); $shipping = ( isset( $args['order_shipping'] ) ? $args['order_shipping'] : false ); $user_roles = ( isset( $args['order_user_roles'] ) ? $args['order_user_roles'] : false ); $coupons = ( isset( $args['order_coupons'] ) ? $args['order_coupons'] : false ); $product = ( isset( $args['order_product'] ) ? $args['order_product'] : false ); $product_category = ( isset( $args['order_category'] ) ? $args['order_category'] : false ); $product_tag = ( isset( $args['order_tag'] ) ? $args['order_tag'] : false ); $product_brand = ( isset( $args['order_brand'] ) ? $args['order_brand'] : false ); $limit_volume = ( isset( $args['limit_volume'] ) ? $args['limit_volume'] : false ); $offset = $args['offset']; $orderby = ( isset( $args['order_orderby'] ) ? $args['order_orderby'] : 'ID' ); $order = ( isset( $args['order_order'] ) ? $args['order_order'] : 'ASC' ); $order_dates_filter = ( isset( $args['order_dates_filter'] ) ? $args['order_dates_filter'] : false ); switch( $order_dates_filter ) { case 'today': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), date( 'd' ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), date( 'd' ) ) ); break; case 'yesterday': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-2 days' ) ), date( 'd', strtotime( '-2 days' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-1 days' ) ), date( 'd', strtotime( '-1 days' ) ) ) ); break; case 'current_week': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'this Monday' ) ), date( 'd', strtotime( 'this Monday' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'next Sunday' ) ), date( 'd', strtotime( 'next Sunday' ) ) ) ); break; case 'last_week': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'last Monday' ) ), date( 'd', strtotime( 'last Monday' ) ) ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( 'last Sunday' ) ), date( 'd', strtotime( 'last Sunday' ) ) ) ); break; case 'current_month': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), 1 ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '+1 month' ) ), 0 ) ); break; case 'last_month': $order_dates_from = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n', strtotime( '-1 month' ) ), 1 ) ); $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, date( 'n' ), 0 ) ); break; case 'manual': $order_dates_from = woo_ce_format_order_date( $args['order_dates_from'] ); $order_dates_to = woo_ce_format_order_date( $args['order_dates_to'] ); break; case 'variable': $order_filter_date_variable = $args['order_dates_filter_variable']; $order_filter_date_variable_length = $args['order_dates_filter_variable_length']; if( $order_filter_date_variable !== false && $order_filter_date_variable_length !== false ) { $timestamp = strtotime( sprintf( '-%d %s', $order_filter_date_variable, $order_filter_date_variable_length ) ); $order_dates_from = date( 'd-m-Y', mktime( date( 'H', $timestamp ), date( 'i', $timestamp ), date( 's', $timestamp ), date( 'n', $timestamp ), date( 'd', $timestamp ), date( 'Y', $timestamp ) ) ); $order_dates_to = date( 'd-m-Y', time() ); unset( $order_filter_date_variable, $order_filter_date_variable_length, $timestamp ); } break; default: $order_dates_from = false; $order_dates_to = false; break; } if( !empty( $order_dates_from ) && !empty( $order_dates_to ) ) { $order_dates_to = explode( '-', $order_dates_to ); // Check that a valid date was provided if( isset( $order_dates_to[0] ) && isset( $order_dates_to[1] ) && isset( $order_dates_to[2] ) ) $order_dates_to = date( 'd-m-Y', mktime( 0, 0, 0, $order_dates_to[1], $order_dates_to[0]+1, $order_dates_to[2] ) ); else $order_dates_to = false; } $order_status = ( isset( $args['order_status'] ) ? $args['order_status'] : array() ); $user_ids = ( isset( $args['order_customer'] ) ? $args['order_customer'] : false ); $billing_country = ( isset( $args['order_billing_country'] ) ? $args['order_billing_country'] : false ); $shipping_country = ( isset( $args['order_shipping_country'] ) ? $args['order_shipping_country'] : false ); $order_items = $args['order_items']; } $post_type = 'shop_order'; $args = array( 'post_type' => $post_type, 'orderby' => $orderby, 'order' => $order, 'offset' => $offset, 'posts_per_page' => $limit_volume, 'fields' => 'ids', 'suppress_filters' => false ); $woocommerce_version = woo_get_woo_version(); // Check if this is a pre-WooCommerce 2.2 instance if( version_compare( $woocommerce_version, '2.2' ) >= 0 ) $args['post_status'] = ( function_exists( 'wc_get_order_statuses' ) ? apply_filters( 'woo_ce_order_post_status', array_keys( wc_get_order_statuses() ) ) : 'any' ); else $args['post_status'] = apply_filters( 'woo_ce_order_post_status', 'publish' ); if( !empty( $order_ids ) ) { $order_ids = explode( ',', $order_ids ); // Check if we're looking up a Sequential Order Number if( class_exists( 'WC_Seq_Order_Number' ) || class_exists( 'WC_Seq_Order_Number_Pro' ) ) { $args['meta_query'][] = array( 'key' => ( class_exists( 'WC_Seq_Order_Number_Pro' ) ? '_order_number_formatted' : '_order_number' ), 'value' => $order_ids ); } else { $size = count( $order_ids ); if( $size > 1 ) $args['post__in'] = array_map( 'absint', $order_ids ); else $args['p'] = absint( $order_ids[0] ); } } if( !empty( $payment ) ) { $args['meta_query'][] = array( 'key' => '_payment_method', 'value' => $payment ); } if( !empty( $order_status ) ) { // Check if this is a WooCommerce 2.2+ instance (new Post Status) if( version_compare( $woocommerce_version, '2.2' ) >= 0 ) { $args['post_status'] = $order_status; if( $export->cron ) { // Something weird is going on so we'll override WordPress on this one $args['post_status'] = implode( ',', $order_status ); $args['suppress_filters'] = false; add_filter( 'posts_where' , 'woo_ce_wp_query_order_where_override' ); } } else { $term_taxonomy = 'shop_order_status'; $args['tax_query'] = array( array( 'taxonomy' => $term_taxonomy, 'field' => 'slug', 'terms' => $order_status ) ); } } if( !empty( $user_ids ) ) { // Check if we're dealing with a string or list of users if( is_string( $user_ids ) ) $user_ids = explode( ',', $user_ids ); $size = count( $user_ids ); $user_emails = array(); foreach( $user_ids as $user_id ) { if( $user = get_userdata( $user_id ) ) $user_emails[] = $user->user_email; } if( !empty( $user_emails ) ) { $args['meta_query'][] = array( 'key' => '_billing_email', 'value' => $user_emails ); } unset( $user_id, $size, $user_emails ); } if( !empty( $billing_country ) ) { $args['meta_query'][] = array( 'key' => '_billing_country', 'value' => $billing_country ); } if( !empty( $shipping_country ) ) { $args['meta_query'][] = array( 'key' => '_shipping_country', 'value' => $shipping_country ); } // Filter Order dates by dropping those outside the date range if( !empty( $order_dates_from ) && !empty( $order_dates_to ) ) { $args['date_query'] = array( array( 'column' => 'post_date_gmt', 'before' => $order_dates_to, 'after' => $order_dates_from, 'inclusive' => true ) ); } if( $order_dates_filter == 'last_export' ) { $args['meta_query'][] = array( 'key' => '_woo_cd_exported', 'value' => 1, 'compare' => 'NOT EXISTS' ); } $orders = array(); // Allow other developers to bake in their own filters $args = apply_filters( 'woo_ce_get_orders_args', $args ); $order_ids = new WP_Query( $args ); // Something weird is going on so we'll override WordPress on this one if( !empty( $order_status ) && $export->cron && version_compare( $woocommerce_version, '2.2' ) >= 0 ) remove_filter( 'posts_where' , 'woo_ce_wp_query_order_where_override' ); if( $order_ids->posts ) { foreach( $order_ids->posts as $order_id ) { // Get WooCommerce Order details $order = woo_ce_get_order_wc_data( $order_id ); // Filter Orders by User Roles $order->user_id = get_post_meta( $order->id, '_customer_user', true ); if( $user_roles ) { $user_ids = array(); $size = count( $export->args['order_user_roles'] ); for( $i = 0; $i < $size; $i++ ) { $args = array( 'role' => $export->args['order_user_roles'][$i], 'fields' => 'ID' ); $user_id = get_users( $args ); $user_ids = array_merge( $user_ids, $user_id ); } if( !in_array( $order->user_id, $user_ids ) ) { unset( $order ); continue; } } // Filter Orders by Coupons $order->coupon_code = woo_ce_get_order_assoc_coupon( $order->id ); if( $coupons ) { $coupon_ids = array(); $size = count( $export->args['order_coupons'] ); for( $i = 0; $i < $size; $i++ ) $coupon_ids[] = get_the_title( $coupons[$i] ); if( !in_array( $order->coupon_code, $coupon_ids ) ) { unset( $order ); continue; } } // Filter Orders by Product if( $product ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $product_ids = array(); foreach( $order_items as $order_item ) { $product_ids = array_merge( $product_ids, (array)$order_item->product_id ); } if( count( array_intersect( $product, $product_ids ) ) == 0 ) { unset( $order ); continue; } } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Category if( $product_category ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = 'product_cat'; $args = array( 'fields' => 'ids' ); $category_ids = array(); foreach( $order_items as $order_item ) { if( $product_categories = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $category_ids = array_merge( $category_ids, $product_categories ); unset( $product_categories ); } } if( count( array_intersect( $product_category, $category_ids ) ) == 0 ) { unset( $order ); continue; } unset( $category_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Tag if( $product_tag ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = 'product_tag'; $args = array( 'fields' => 'ids' ); $tag_ids = array(); foreach( $order_items as $order_item ) { if( $product_tags = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $tag_ids = array_merge( $tag_ids, $product_tags ); unset( $product_tags ); } } if( empty( $tag_ids ) || count( array_intersect( $product_tag, $tag_ids ) ) == 0 ) { unset( $order ); continue; } unset( $tag_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Product Brand if( $product_brand ) { if( $order_items = woo_ce_get_order_item_ids( $order->id ) ) { $term_taxonomy = apply_filters( 'woo_ce_brand_term_taxonomy', 'product_brand' ); $args = array( 'fields' => 'ids' ); $brand_ids = array(); foreach( $order_items as $order_item ) { if( $product_brands = wp_get_post_terms( $order_item->product_id, $term_taxonomy, $args ) ) { $brand_ids = array_merge( $brand_ids, $product_brands ); unset( $product_brands ); } } if( empty( $brand_ids ) || count( array_intersect( $product_brand, $brand_ids ) ) == 0 ) { unset( $order ); continue; } unset( $brand_ids ); } else { // If the Order has no Order Items assigned to it we can safely remove it from the export unset( $order ); continue; } unset( $order_items ); } // Filter Orders by Shipping Method if( $shipping ) { $shipping_id = woo_ce_get_order_assoc_shipping_method_id( $order->id ); if( !in_array( $shipping_id, $shipping ) ) { unset( $order ); continue; } unset( $shipping_id ); } $orders[] = $order_id; // Mark this Order as exported if Since last export Date filter is used if( $order_dates_filter == 'last_export' ) { update_post_meta( $order_id, '_woo_cd_exported', 1 ); // Add an Order Note $note = __( 'Order was exported successfully.', 'woo_ce' ); $order->add_order_note( $note ); unset( $note ); } } $export->total_rows = count( $orders ); unset( $order_ids, $order_id ); } switch( $export_type ) { case 'order': if( WOO_CD_DEBUG !== true ) { if( $order_dates_filter == 'last_export' ) { // Save the Order ID's list to a WordPress Transient incase the export fails woo_ce_update_option( 'exported', $orders ); } } return $orders; break; case 'customer': $customers = array(); if( !empty( $orders ) ) { foreach( $orders as $order_id ) { $order = woo_ce_get_order_data( $order_id, 'customer', $export->args ); if( $duplicate_key = woo_ce_is_duplicate_customer( $customers, $order ) ) { $customers[$duplicate_key]->total_spent = $customers[$duplicate_key]->total_spent + woo_ce_format_price( get_post_meta( $order_id, '_order_total', true ) ); $customers[$duplicate_key]->total_orders++; if( strtolower( $order->payment_status ) == 'completed' ) $customers[$duplicate_key]->completed_orders++; } else { $customers[$order_id] = $order; $customers[$order_id]->total_spent = woo_ce_format_price( get_post_meta( $order_id, '_order_total', true ) ); $customers[$order_id]->completed_orders = 0; if( strtolower( $order->payment_status ) == 'completed' ) $customers[$order_id]->completed_orders = 1; $customers[$order_id]->total_orders = 1; } } } return $customers; break; } }