Snippets

Visser Labs Adding custom Order Item fields to the Order export type

Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+function custom_woo_ce_order_fields( $fields ) {
+
+	$fields[] = array(
+		'name' => 'order_items_departure_date',
+		'label' => __( 'Order Items: Departure Date', 'woo_ce' ),
+		'hover' => __( 'Custom Field within functions.php', 'woo_ce' )
+	);
+
+	return $fields;
+
+}
+add_filter( 'woo_ce_order_fields', 'custom_woo_ce_order_fields' );
+
+function custom_woo_ce_order_item_custom_meta( $order_item, $meta_key, $meta_value ) {
+
+    // The saved Order Item meta name is "departure_date"
+	if( $meta_key == 'departure_date' ) {
+		$order_item->departure_date = $meta_value;
+	}
+
+	return $order_item;
+
+}
+add_filter( 'woo_ce_order_item_custom_meta', 'custom_woo_ce_order_item_custom_meta', 10, 3 );
+
+// Adding support for the "Place Order Items within a grouped single Order row" Order Items Formatting rule
+function custom_woo_ce_order_items_combined( $order ) {
+
+	global $export;
+
+	if( $order->order_items ) {
+		foreach( $order->order_items as $order_item ) {
+			if( isset( $order_item->departure_date ) )
+				$order->order_items_departure_date .= $order_item->departure_date . $export->category_separator;
+		}
+		if( isset( $order->order_items_departure_date ) )
+			$order->order_items_departure_date = substr( $order->order_items_departure_date, 0, -1 );
+	}
+
+	return $order;
+
+}
+add_filter( 'woo_ce_order_items_combined', 'custom_woo_ce_order_items_combined' );
+
+// Adding support for the "Place each Order Item within their own Order row" Order Items Formatting rule
+function custom_woo_ce_order_items_individual( $order, $order_item ) {
+
+	if( $order->order_items ) {
+		if( isset( $order_item->departure_date ) )
+			$order->order_items_departure_date = $order_item->departure_date;
+	}
+	return $order;
+
+}
+add_filter( 'woo_ce_order_items_individual', 'custom_woo_ce_order_items_individual', 10, 2 );
+
+// Adding support for the "Place Order Items on individual cells within a single Order row" Order Items Formatting rule
+function custom_woo_ce_extend_order_items_unique_columns( $fields = array(), $i = 0, $original_columns = array() ) {
+
+	if( isset( $original_columns[sprintf( 'order_item_%d_departure_date', $i )] ) )
+		$fields[] = sprintf( __( 'Order Item #%d: Departure Date', 'woo_ce' ), $i );
+	return $fields;
+
+}
+add_filter( 'woo_ce_unique_order_item_columns', 'custom_woo_ce_extend_order_items_unique_columns', 10, 3 );
+
+function custom_woo_ce_extend_order_items_unique( $order, $i = 0, $order_item = array() ) {
+
+	$order->{sprintf( 'order_item_%d_departure_date', $i )} = $order_item->departure_date;
+	return $order;
+
+}
+add_filter( 'woo_ce_order_items_unique', 'custom_woo_ce_extend_order_items_unique', 10, 3 );
+
+function custom_woo_ce_unique_order_item_fields_on( $fields = array(), $i = 0 ) {
+
+	$fields[sprintf( 'order_item_%d_departure_date', $i )] = 'on';
+	return $fields;
+
+}
+add_filter( 'woo_ce_add_unique_order_item_fields_on', 'custom_woo_ce_unique_order_item_fields_on', 10, 2 );
+
+function custom_woo_ce_extend_order_items_unique_fields_exclusion( $excluded_fields = array(), $fields = '' ) {
+
+	$excluded_fields[] = 'order_items_departure_date';
+	return $excluded_fields;
+
+}
+add_filter( 'woo_ce_add_unique_order_item_fields_exclusion', 'custom_woo_ce_extend_order_items_unique_fields_exclusion', 10, 2 );
+?>