gravityflowwoocommerce_new_entry

The gravityflowwoocommerce_new_entry can be used to add values from a WooCommerce order to entry fields. It is useful when you cannot map custom WooCommerce order data to entry fields in the WooCommerce settings tab.

Parameters 

$new_entry array

The result to be overridden.

$order WC_Order

WooCommerce order object.

$form array

The form object.

Example 1

The following example shows how to map WooCommerce product variation (attribute) and custom checkout field (created by the Checkout Field Editor plugin) to entry fields.

add_filter( 'gravityflowwoocommerce_new_entry', 'my_gravityflowwoocommerce_new_entry', 10, 3 );
/**
 * Add product variation to the entry.
 *
 * @param array $new_entry The entry object.
 * @param WC_Order $order WooCommerce order object.
 * @param array $form The Form object.
 *
 * @return array
 */
function my_gravityflowwoocommerce_new_entry( $new_entry, $order, $form ) {
	// Say it's the form #63 you'd like to customize.
	if ( $form['id'] == 63 ) {
		if ( count( $order->get_items() ) > 0 ) {
			foreach ( $order->get_items() as $item ) {
				if ( $item->is_type( 'line_item' ) ) {
					$product = $item->get_product();
					// service-period is the variation attribute.
					$attribute = $product->get_attribute( 'service-period' );
					// Map service period to field #1.
					$new_entry[1] = $attribute;
				}
			}
		}
		// Map website URL (a custom checkout field) to field #5.
		$new_entry[5] = get_post_meta( $order->get_id(), 'website_url', true );
	}

	return $new_entry;
}

Example 2

The following example shows how to set the entry user (the created_by field) to the current logged in user when a new entry is created by a new WooCommerce order.

add_filter( 'gravityflowwoocommerce_new_entry', 'my_gravityflowwoocommerce_new_entry', 10, 3 );
/**
 * Setup the entry user (the created_by field of an entry).
 *
 * @param array $new_entry The entry object.
 * @param WC_Order $order WooCommerce order object.
 * @param array $form The Form object.
 *
 * @return array
 */
function my_gravityflowwoocommerce_new_entry( $new_entry, $order, $form ) {
	if ( $form['id'] === 32 ) {
		$new_entry['created_by'] = get_current_user_id();
	}

	return $new_entry;
}

Since 

This filter was added in version 1.0.

Placement 

This code should be placed in the functions.php file of your active theme.