예제 #1
0
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;
	}

}
예제 #2
0
function woo_ce_get_order_data( $order_id = 0, $export_type = 'order', $args = array(), $fields = array() ) {

	global $export;

	$defaults = array(
		'order_items' => 'combined',
		'order_items_types' => array_keys( woo_ce_get_order_items_types() )
	);
	$args = wp_parse_args( $args, $defaults );

	// Get WooCommerce Order details
	$order = woo_ce_get_order_wc_data( $order_id );

	$order->ID = ( isset( $order->id ) ? $order->id : $order_id );
	$order->payment_status = $order->status;
	// Check if this is a pre-WooCommerce 2.2 instance
	$woocommerce_version = woo_get_woo_version();
	$order->post_status = woo_ce_format_post_status( $order->post_status );
	$order->user_id = get_post_meta( $order_id, '_customer_user', true );
	if( $order->user_id == 0 )
		$order->user_id = '';
	$order->user_name = woo_ce_get_username( $order->user_id );
	$order->user_role = woo_ce_format_user_role_label( woo_ce_get_user_role( $order->user_id ) );
	$order->purchase_total = get_post_meta( $order_id, '_order_total', true );
	$order->order_currency = get_post_meta( $order_id, '_order_currency', true );
	$order->billing_first_name = get_post_meta( $order_id, '_billing_first_name', true );
	$order->billing_last_name = get_post_meta( $order_id, '_billing_last_name', true );
	if( empty( $order->billing_first_name ) && empty( $order->billing_first_name ) )
		$order->billing_full_name = '';
	else
		$order->billing_full_name = $order->billing_first_name . ' ' . $order->billing_last_name;
	$order->billing_company = get_post_meta( $order_id, '_billing_company', true );
	$order->billing_address = '';
	$order->billing_address_1 = get_post_meta( $order_id, '_billing_address_1', true );
	$order->billing_address_2 = get_post_meta( $order_id, '_billing_address_2', true );
	if( !empty( $order->billing_address_2 ) )
		$order->billing_address = sprintf( apply_filters( 'woo_ce_get_order_data_billing_address', '%s %s' ), $order->billing_address_1, $order->billing_address_2 );
	else
		$order->billing_address = $order->billing_address_1;
	$order->billing_city = get_post_meta( $order_id, '_billing_city', true );
	$order->billing_postcode = get_post_meta( $order_id, '_billing_postcode', true );
	$order->billing_state = get_post_meta( $order_id, '_billing_state', true );
	$order->billing_country = get_post_meta( $order_id, '_billing_country', true );
	$order->billing_state_full = woo_ce_expand_state_name( $order->billing_country, $order->billing_state );
	$order->billing_country_full = woo_ce_expand_country_name( $order->billing_country );
	$order->billing_phone = get_post_meta( $order_id, '_billing_phone', true );
	$order->billing_email = get_post_meta( $order_id, '_billing_email', true );
	// If the e-mail address is empty check if the Order has a User assigned to it
	if( empty( $order->billing_email ) ) {
		// Check if a User ID has been assigned
		if( !empty( $order->user_id ) ) {
			$user = woo_ce_get_user_data( $order->user_id );
			// Check if the User is valid and e-mail assigned to User
			if( isset( $user->email ) )
				$order->billing_email = $user->email;
			unset( $user );
		}
	}
	$order->shipping_first_name = get_post_meta( $order_id, '_shipping_first_name', true );
	$order->shipping_last_name = get_post_meta( $order_id, '_shipping_last_name', true );
	if( empty( $order->shipping_first_name ) && empty( $order->shipping_last_name ) )
		$order->shipping_full_name = '';
	else
		$order->shipping_full_name = $order->shipping_first_name . ' ' . $order->shipping_last_name;
	$order->shipping_company = get_post_meta( $order_id, '_shipping_company', true );
	$order->shipping_address = '';
	$order->shipping_address_1 = get_post_meta( $order_id, '_shipping_address_1', true );
	$order->shipping_address_2 = get_post_meta( $order_id, '_shipping_address_2', true );
	if( !empty( $order->billing_address_2 ) )
		$order->shipping_address = sprintf( apply_filters( 'woo_ce_get_order_data_shipping_address', '%s %s' ), $order->shipping_address_1, $order->shipping_address_2 );
	else
		$order->shipping_address = $order->shipping_address_1;
	$order->shipping_city = get_post_meta( $order_id, '_shipping_city', true );
	$order->shipping_postcode = get_post_meta( $order_id, '_shipping_postcode', true );
	$order->shipping_state = get_post_meta( $order_id, '_shipping_state', true );
	$order->shipping_country = get_post_meta( $order_id, '_shipping_country', true );
	$order->shipping_state_full = woo_ce_expand_state_name( $order->shipping_country, $order->shipping_state );
	$order->shipping_country_full = woo_ce_expand_country_name( $order->shipping_country );
	$order->shipping_phone = get_post_meta( $order_id, '_shipping_phone', true );

	if( $export_type == 'order' ) {

		$order->post_id = $order->purchase_id = $order_id;
		$order->order_discount = get_post_meta( $order_id, '_cart_discount', true );
		$order->coupon_code = woo_ce_get_order_assoc_coupon( $order_id );
		if( !empty( $order->coupon_code ) ) {
			$coupon = get_page_by_title( $order->coupon_code, OBJECT, 'shop_coupon' );
			if( $coupon !== null )
				$order->coupon_description = $coupon->post_excerpt;
			unset( $coupon );
		}
		$order->order_sales_tax = get_post_meta( $order_id, '_order_tax', true );
		$order->order_shipping_tax = get_post_meta( $order_id, '_order_shipping_tax', true );
		$order->purchase_total_tax = ( $order->order_sales_tax - $order->order_shipping_tax );
		$order->order_excl_tax = ( $order->purchase_total - $order->purchase_total_tax );
		// Order Tax Percentage - Order Total - Total Tax / Total Tax
		if( !empty( $order->purchase_total_tax ) && !empty( $order->purchase_total ) )
			$order->order_tax_percentage = absint( ( $order->purchase_total - $order->purchase_total_tax ) / $order->purchase_total_tax ) . '%';
		$order->purchase_total = woo_ce_format_price( $order->purchase_total, $order->order_currency );
		$order->order_sales_tax = woo_ce_format_price( $order->order_sales_tax, $order->order_currency );
		$order->order_shipping_tax = woo_ce_format_price( $order->order_shipping_tax, $order->order_currency );
		$order->purchase_subtotal = woo_ce_format_price( $order->order_excl_tax, $order->order_currency );
		$order->order_discount = woo_ce_format_price( $order->order_discount, $order->order_currency );
		$order->order_excl_tax = woo_ce_format_price( $order->order_excl_tax, $order->order_currency );
		$order->payment_status = woo_ce_format_order_status( $order->payment_status );
		$order->payment_gateway_id = get_post_meta( $order_id, '_payment_method', true );
		$order->payment_gateway = woo_ce_format_order_payment_gateway( $order->payment_gateway_id );
		// WooCommerce 2.1 stores the shipping method in cart items, includes fallback support
		if( method_exists( $order, 'get_shipping_method' ) ) {
			$order->shipping_method_id = woo_ce_get_order_assoc_shipping_method_id( $order_id );
			$order->shipping_method = $order->get_shipping_method();
		} else {
			$order->shipping_method_id = get_post_meta( $order_id, '_shipping_method', true );
			$order->shipping_method = '';
		}
		$order->shipping_cost = woo_ce_format_price( get_post_meta( $order_id, '_order_shipping', true ), $order->order_currency );
		$order->shipping_weight = '';
		$order->order_key = get_post_meta( $order_id, '_order_key', true );
		$order->purchase_date = woo_ce_format_date( $order->order_date );
		$order->purchase_time = mysql2date( 'H:i:s', $order->order_date );
		$order->ip_address = woo_ce_format_ip_address( get_post_meta( $order_id, '_customer_ip_address', true ) );
		$order->browser_agent = get_post_meta( $order_id, '_customer_user_agent', true );
		$order->has_downloads = 0;
		$order->has_downloaded = 0;
		// Order Downloads
		if( $order_downloads = woo_ce_get_order_assoc_downloads( $order_id ) ) {
			$order->has_downloads = 1;
			foreach( $order_downloads as $order_download ) {
				// Check if any download permissions have counts against them
				if( $order_download->download_count > 0 ) {
					$order->has_downloaded = 1;
					break;
				}
			}
		}
		unset( $order_downloads, $order_download );
		$order->has_downloads = woo_ce_format_switch( $order->has_downloads );
		$order->has_downloaded = woo_ce_format_switch( $order->has_downloaded );
		$order->customer_notes = '';
		$order->order_notes = '';
		$order->total_quantity = 0;
		$order->total_order_items = 0;
		// Order Notes
		if( $order_notes = woo_ce_get_order_assoc_notes( $order_id ) ) {
			if( WOO_CD_DEBUG )
				$order->order_notes = implode( $export->category_separator, $order_notes );
			else
				$order->order_notes = implode( "\n", $order_notes );
			unset( $order_notes );
		}
		// Customer Notes
		if( $order_notes = woo_ce_get_order_assoc_notes( $order_id, 'customer_note' ) ) {
			if( WOO_CD_DEBUG )
				$order->customer_notes = implode( $export->category_separator, $order_notes );
			else
				$order->customer_notes = implode( "\n", $order_notes );
			unset( $order_notes );
		}
		if( $order->order_items = woo_ce_get_order_items( $order_id, $args['order_items_types'] ) ) {
			$order->total_order_items = count( $order->order_items );
			if( $args['order_items'] == 'combined' ) {
				$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 = '';
				if( !empty( $order->order_items ) ) {
					foreach( $order->order_items as $order_item ) {
						if( empty( $order_item->sku ) )
							$order_item->sku = '';
						$order->order_items_product_id .= $order_item->product_id . $export->category_separator;
						$order->order_items_variation_id .= $order_item->variation_id . $export->category_separator;
						$order->order_items_sku .= $order_item->sku . $export->category_separator;
						$order->order_items_name .= $order_item->name . $export->category_separator;
						$order->order_items_variation .= $order_item->variation . $export->category_separator;
						$order->order_items_description .= woo_ce_format_description_excerpt( $order_item->description ) . $export->category_separator;
						$order->order_items_excerpt .= woo_ce_format_description_excerpt( $order_item->excerpt ) . $export->category_separator;
						$order->order_items_tax_class .= $order_item->tax_class . $export->category_separator;
						$order->total_quantity += $order_item->quantity;
						if( empty( $order_item->quantity ) && '0' != $order_item->quantity )
							$order_item->quantity = '';
						$order->order_items_quantity .= $order_item->quantity . $export->category_separator;
						$order->order_items_total .= $order_item->total . $export->category_separator;
						$order->order_items_subtotal .= $order_item->subtotal . $export->category_separator;
						$order->order_items_rrp .= $order_item->rrp . $export->category_separator;
						$order->order_items_stock .= $order_item->stock . $export->category_separator;
						$order->order_items_tax .= $order_item->tax . $export->category_separator;
						$order->order_items_tax_subtotal .= $order_item->tax_subtotal . $export->category_separator;
						$order->order_items_type .= $order_item->type . $export->category_separator;
						$order->order_items_category .= $order_item->category . $export->category_separator;
						$order->order_items_tag .= $order_item->tag . $export->category_separator;
						$order->order_items_total_sales .= $order_item->total_sales . $export->category_separator;
						$order->order_items_weight .= $order_item->weight . $export->category_separator;
						$order->order_items_total_weight .= $order_item->total_weight . $export->category_separator;
						// 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 = substr( $order->order_items_product_id, 0, -1 );
					$order->order_items_variation_id = substr( $order->order_items_variation_id, 0, -1 );
					$order->order_items_sku = substr( $order->order_items_sku, 0, -1 );
					$order->order_items_name = substr( $order->order_items_name, 0, -1 );
					$order->order_items_variation = substr( $order->order_items_variation, 0, -1 );
					$order->order_items_description = substr( $order->order_items_description, 0, -1 );
					$order->order_items_excerpt = substr( $order->order_items_excerpt, 0, -1 );
					$order->order_items_tax_class = substr( $order->order_items_tax_class, 0, -1 );
					$order->order_items_quantity = substr( $order->order_items_quantity, 0, -1 );
					$order->order_items_total = substr( $order->order_items_total, 0, -1 );
					$order->order_items_subtotal = substr( $order->order_items_subtotal, 0, -1 );
					$order->order_items_rrp = substr( $order->order_items_rrp, 0, -1 );
					$order->order_items_stock = substr( $order->order_items_stock, 0, -1 );
					$order->order_items_tax = substr( $order_item->tax, 0, -1 );
					$order->order_items_tax_subtotal = substr( $order_item->tax_subtotal, 0, -1 );
					$order->order_items_type = substr( $order->order_items_type, 0, -1 );
					$order->order_items_category = substr( $order->order_items_category, 0, -1 );
					$order->order_items_tag = substr( $order->order_items_tag, 0, -1 );
					$order->order_items_total_sales = substr( $order->order_items_total_sales, 0, -1 );
					$order->order_items_weight = substr( $order->order_items_weight, 0, -1 );
					$order->order_items_total_weight = substr( $order->order_items_total_weight, 0, -1 );
				}
				$order = apply_filters( 'woo_ce_order_items_combined', $order );
			} else if( $args['order_items'] == 'unique' ) {
				if( !empty( $order->order_items ) ) {
					$i = 1;
					foreach( $order->order_items as $order_item ) {
						$order->{sprintf( 'order_item_%d_product_id', $i )} = $order_item->product_id;
						$order->{sprintf( 'order_item_%d_variation_id', $i )} = $order_item->variation_id;
						$order->{sprintf( 'order_item_%d_sku', $i )} = ( empty( $order_item->sku ) == false ? $order_item->sku : '' );
						$order->{sprintf( 'order_item_%d_name', $i )} = $order_item->name;
						$order->{sprintf( 'order_item_%d_variation', $i )} = $order_item->variation;
						$order->{sprintf( 'order_item_%d_description', $i )} = $order_item->description;
						$order->{sprintf( 'order_item_%d_excerpt', $i )} = $order_item->excerpt;
						$order->{sprintf( 'order_item_%d_tax_class', $i )} = $order_item->tax_class;
						$order->total_quantity += $order_item->quantity;
						if( empty( $order_item->quantity ) && '0' != $order_item->quantity )
							$order_item->quantity = '';
						$order->{sprintf( 'order_item_%d_quantity', $i )} = $order_item->quantity;
						$order->{sprintf( 'order_item_%d_total', $i )} = $order_item->total;
						$order->{sprintf( 'order_item_%d_subtotal', $i )} = $order_item->subtotal;
						$order->{sprintf( 'order_item_%d_rrp', $i )} = $order_item->rrp;
						$order->{sprintf( 'order_item_%d_stock', $i )} = $order_item->stock;
						$order->{sprintf( 'order_item_%d_tax', $i )} = $order_item->tax;
						$order->{sprintf( 'order_item_%d_tax_subtotal', $i )} = $order_item->tax_subtotal;
						$order->{sprintf( 'order_item_%d_type', $i )} = $order_item->type;
						$order->{sprintf( 'order_item_%d_category', $i )} = $order_item->category;
						$order->{sprintf( 'order_item_%d_tag', $i )} = $order_item->tag;
						$order->{sprintf( 'order_item_%d_total_sales', $i )} = $order_item->total_sales;
						$order->{sprintf( 'order_item_%d_weight', $i )} = $order_item->weight;
						$order->{sprintf( 'order_item_%d_total_weight', $i )} = $order_item->total_weight;
						// Add Order Item weight to Shipping Weight
						if( $order_item->total_weight != '' )
							$order->shipping_weight = $order->shipping_weight + $order_item->total_weight;
						$order = apply_filters( 'woo_ce_order_items_unique', $order, $i, $order_item );
						$i++;
					}
				}
			}
		}

		// Custom Order fields
		$custom_orders = woo_ce_get_option( 'custom_orders', '' );
		if( !empty( $custom_orders ) ) {
			foreach( $custom_orders as $custom_order ) {
				if( !empty( $custom_order ) ) {
					$order->{$custom_order} = woo_ce_format_custom_meta( get_post_meta( $order_id, $custom_order, true ) );
				}
			}
		}

	} else if( $export_type = 'customer' ) {

		// Check if the Order has a User assigned to it
		if( !empty( $order->user_id ) ) {

			// Load up the User data as other Plugins will use it too
			$user = woo_ce_get_user_data( $order->user_id );

			// WooCommerce Follow-Up Emails - http://www.woothemes.com/products/follow-up-emails/
			if( class_exists( 'FollowUpEmails' ) ) {

				global $wpdb;

				if( isset( $user->email ) ) {
					$followup_optout_sql = $wpdb->prepare( "SELECT `id` FROM `" . $wpdb->prefix . "followup_email_excludes` WHERE `email` = %s LIMIT 1", $user->email );
					$order->followup_optout = $wpdb->get_var( $followup_optout_sql );
				}

			}

			// Custom User fields
			$custom_users = woo_ce_get_option( 'custom_users', '' );
			if( !empty( $custom_users ) ) {
				foreach( $custom_users as $custom_user ) {
					if( !empty( $custom_user ) && !isset( $order->{$custom_user} ) ) {
						$order->{$custom_user} = woo_ce_format_custom_meta( get_user_meta( $order->user_id, $custom_user, true ) );
					}
				}
			}
			unset( $custom_users, $custom_user );

			// Clean up
			unset( $user );

		}

		// Custom Customer fields
		$custom_customers = woo_ce_get_option( 'custom_customers', '' );
		if( !empty( $custom_customers ) ) {
			foreach( $custom_customers as $custom_customer ) {
				if( !empty( $custom_customer ) )
					$order->{$custom_customer} = esc_attr( get_user_meta( $order->user_id, $custom_customer, true ) );
			}
		}

	}

	$order = apply_filters( 'woo_ce_order', $order, $order_id );

	// Trim back the Order just to requested export fields
	if( !empty( $fields ) ) {
		$fields[] = 'id';
		if( $args['order_items'] == 'individual' )
			$fields[] = 'order_items';
		if( !empty( $order ) ) {
			foreach( $order as $key => $data ) {
				if( !in_array( $key, $fields ) )
					unset( $order->$key );
			}
		}
	}

	return $order;

}
예제 #3
0
function woo_ce_export_dataset($export_type = null, &$output = null)
{
    global $export;
    $separator = $export->delimiter;
    $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_CE_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);
                }
            }
            $export->total_columns = $size = 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);
                // Generate the export headers
                if (in_array($export->export_format, array('csv'))) {
                    for ($i = 0; $i < $size; $i++) {
                        if ($i == $size - 1) {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . "\n";
                        } else {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . $separator;
                        }
                    }
                }
                $weight_unit = get_option('woocommerce_weight_unit');
                $dimension_unit = get_option('woocommerce_dimension_unit');
                $height_unit = $dimension_unit;
                $width_unit = $dimension_unit;
                $length_unit = $dimension_unit;
                if (!empty($export->fields)) {
                    foreach ($products as $product) {
                        $product = woo_ce_get_product_data($product, $export->args);
                        foreach ($export->fields as $key => $field) {
                            if (isset($product->{$key})) {
                                if (is_array($field)) {
                                    foreach ($field as $array_key => $array_value) {
                                        if (!is_array($array_value)) {
                                            if (in_array($export->export_format, array('csv'))) {
                                                $output .= woo_ce_escape_csv_value($array_value, $export->delimiter, $export->escape_formatting);
                                            }
                                        }
                                    }
                                } else {
                                    if (in_array($export->export_format, array('csv'))) {
                                        $output .= woo_ce_escape_csv_value($product->{$key}, $export->delimiter, $export->escape_formatting);
                                    }
                                }
                            }
                            if (in_array($export->export_format, array('csv'))) {
                                $output .= $separator;
                            }
                        }
                        if (in_array($export->export_format, array('csv'))) {
                            $output = substr($output, 0, -1) . "\n";
                        }
                    }
                }
                unset($products, $product);
            }
            $export->data_memory_end = woo_ce_current_memory_usage();
            break;
            // Categories
        // 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 = $size = 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);
                // Generate the export headers
                if (in_array($export->export_format, array('csv'))) {
                    for ($i = 0; $i < $size; $i++) {
                        if ($i == $size - 1) {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . "\n";
                        } else {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . $separator;
                        }
                    }
                }
                if (!empty($export->fields)) {
                    foreach ($categories as $category) {
                        foreach ($export->fields as $key => $field) {
                            if (isset($category->{$key})) {
                                if (in_array($export->export_format, array('csv'))) {
                                    $output .= woo_ce_escape_csv_value($category->{$key}, $export->delimiter, $export->escape_formatting);
                                }
                            }
                            if (in_array($export->export_format, array('csv'))) {
                                $output .= $separator;
                            }
                        }
                        if (in_array($export->export_format, array('csv'))) {
                            $output = substr($output, 0, -1) . "\n";
                        }
                    }
                }
                unset($categories, $category);
            }
            $export->data_memory_end = woo_ce_current_memory_usage();
            break;
            // Tags
        // 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 = $size = 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);
                // Generate the export headers
                if (in_array($export->export_format, array('csv'))) {
                    for ($i = 0; $i < $size; $i++) {
                        if ($i == $size - 1) {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . "\n";
                        } else {
                            $output .= woo_ce_escape_csv_value($export->columns[$i], $export->delimiter, $export->escape_formatting) . $separator;
                        }
                    }
                }
                if (!empty($export->fields)) {
                    foreach ($tags as $tag) {
                        foreach ($export->fields as $key => $field) {
                            if (isset($tag->{$key})) {
                                if (in_array($export->export_format, array('csv'))) {
                                    $output .= woo_ce_escape_csv_value($tag->{$key}, $export->delimiter, $export->escape_formatting);
                                }
                            }
                            if (in_array($export->export_format, array('csv'))) {
                                $output .= $separator;
                            }
                        }
                        if (in_array($export->export_format, array('csv'))) {
                            $output = substr($output, 0, -1) . "\n";
                        }
                    }
                }
                unset($tags, $tag);
            }
            $export->data_memory_end = woo_ce_current_memory_usage();
            break;
            // Users
        // 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 = $size = count($export->columns);
            $export->data_memory_start = woo_ce_current_memory_usage();
            if ($users = woo_ce_get_users($export->args)) {
                // Generate the export headers
                if (in_array($export->export_format, array('csv'))) {
                    $i = 0;
                    foreach ($export->columns as $column) {
                        if ($i == $size - 1) {
                            $output .= woo_ce_escape_csv_value($column, $export->delimiter, $export->escape_formatting) . "\n";
                        } else {
                            $output .= woo_ce_escape_csv_value($column, $export->delimiter, $export->escape_formatting) . $separator;
                        }
                        $i++;
                    }
                }
                if (!empty($export->fields)) {
                    foreach ($users as $user) {
                        $user = woo_ce_get_user_data($user, $export->args);
                        foreach ($export->fields as $key => $field) {
                            if (isset($user->{$key})) {
                                if (in_array($export->export_format, array('csv'))) {
                                    $output .= woo_ce_escape_csv_value($user->{$key}, $export->delimiter, $export->escape_formatting);
                                }
                            }
                            if (in_array($export->export_format, array('csv'))) {
                                $output .= $separator;
                            }
                        }
                        if (in_array($export->export_format, array('csv'))) {
                            $output = substr($output, 0, -1) . "\n";
                        }
                    }
                }
                unset($users, $user);
            }
            $export->data_memory_end = woo_ce_current_memory_usage();
            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_CE_PREFIX . '_running');
    // Check that the export file is populated, export columns have been assigned and rows counted
    if ($output && $export->total_rows && $export->total_columns) {
        if (in_array($export->export_format, array('csv'))) {
            $output = woo_ce_file_encoding($output);
            if ($export->export_format == 'csv' && $export->bom && WOO_CE_DEBUG == false) {
                $output = "" . $output;
            }
        }
        if (WOO_CE_DEBUG && !$export->cron) {
            $response = set_transient(WOO_CE_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_ce_admin_notice')) {
                    woo_ce_admin_notice($message, 'error');
                } else {
                    error_log(sprintf('[store-exporter] woo_ce_export_dataset() - %s', $message));
                }
            }
        } else {
            return $output;
        }
    }
}
function woo_ce_get_subscription_data( $subscription ) {

	$order = woo_ce_get_order_wc_data( $subscription['order_id'] );
	$order_item = woo_ce_get_subscription_order_item( $subscription['order_id'], $subscription['product_id'] );
	$product = woo_ce_get_subscription_product( $order, $order_item );
	$subscription['key'] = woo_ce_get_subscription_key( $subscription['order_id'], $subscription['product_id'] );
	$subscription['name'] = $order_item['name'];
	if( isset( $product->variation_data ) )
		$subscription['name'] = ( function_exists( 'woocommerce_get_formatted_variation' ) ? woocommerce_get_formatted_variation( $product->variation_data, true ) : $subscription['name'] );
	else
		$subscription['variation_id'] = '';
	$subscription['order_status'] = woo_ce_format_order_status( $order->status );
	$subscription['post_status'] = ucwords( $order->post_status );
	$subscription['user_id'] = get_post_meta( $subscription['order_id'], '_customer_user', true );
	$subscription['user'] = woo_ce_get_username( $subscription['user_id'] );
	$user = woo_ce_get_user_data( $subscription['user_id'] );
	$subscription['email'] = ( isset( $user->email ) ? $user->email : '' );
	unset( $user );
	$subscription['status'] = ( isset( $subscription_statuses[$subscription['status']] ) ? $subscription_statuses[$subscription['status']] : $subscription['status'] );
	$subscription['start_date'] = date_i18n( woocommerce_date_format(), strtotime( $order_item['subscription_start_date'] ) );
	$subscription['expiration'] = ( !empty( $subscription['expiry_date'] ) ? woo_ce_format_subscription_date( $subscription['expiry_date'] ) : __( 'Never', 'woocommerce-subscriptions' ) );
	$subscription['end_date'] = ( !empty( $order_item['subscription_expiry_date'] ) ? date_i18n( woocommerce_date_format(), strtotime( $order_item['subscription_expiry_date'] ) ) : __( 'Not yet ended', 'woocommerce-subscriptions' ) );
	$subscription['trial_end_date'] = ( !empty( $order_item['subscription_trial_expiry_date'] ) ? date_i18n( woocommerce_date_format(), strtotime( $order_item['subscription_trial_expiry_date'] ) ) : '-' );
	$subscription['last_payment'] = ( !empty( $subscription['last_payment_date'] ) ? woo_ce_format_subscription_date( $subscription['last_payment_date'] ) : '-' );
	$subscription['next_payment'] = woo_ce_get_subscription_next_payment( $subscription['key'], $subscription['user_id'] );
	$subscription['renewals'] = woo_ce_get_subscription_renewals( $subscription['order_id'] );
	if( method_exists( $product, 'get_sku' ) )
		$subscription['product_sku'] = $product->get_sku();
	$subscription['coupon'] = woo_ce_get_order_assoc_coupon( $subscription['order_id'] );
	return $subscription;

}