gravityflowwoocommerce_new_entry

Description

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

ParameterTypeDefinition
$new_entryEntryThe result to be overridden.
$orderWooCommerce OrderWooCommerce order object.
$formFormThe form object.

Examples

How to map WooCommerce product variation

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;
}

Set created_by to the current logged in user

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 can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?