Map Order Data to Entry

Populate fields that are linked to a WooCommerce product using GS Product Configurator with values from the WooCommerce order.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gspc-map-order-data-to-entry.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * Gravity Shop // Product Configurator // Map Order Data to Entry
 * https://gravitywiz.com/documentation/gravity-shop-product-configurator/
 *
 * Populate fields that are linked to a WooCommerce product using GS Product Configurator with values from the WooCommerce order.
 *
 * Instructions Video: https://www.loom.com/share/423b1e3835dc4757aae26a3efe9351b0
 *
 * Plugin Name:  GSPC Map Order Data to Entry
 * Plugin URI:   https://gravitywiz.com/documentation/gravity-shop-product-configurator/
 * Description:  Populate fields that are linked to a WooCommerce product using GS Product Configurator with values from the WooCommerce order.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   https://gravitywiz.com
 */
class GSPC_Map_Order_Data_to_Entry {

	private $_args = array();

	public function __construct( $args = array() ) {

		$this->_args = wp_parse_args( $args, array(
			'form_id'   => false,
			'field_map' => array(),
		) );

		if ( empty( $this->_args['form_id'] ) ) {
			error_log( 'GSPC_Map_Order_Data_to_Entry requires a form_id.' );
			return;
		}

		add_action( 'init', array( $this, 'init' ) );
	}

	public function init() {

		if ( ! property_exists( 'GFCommon', 'version' ) || 
		     ! class_exists( '\GS_Product_Configurator\WC_Order_Item' ) || 
		     ! class_exists( 'GFAPI' ) ) {
			return;
		}

		add_action( 'woocommerce_checkout_order_processed', array( $this, 'map_order_data_to_entries' ), 10, 3 );
	}

	public function map_order_data_to_entries( $order_id, $posted_data, $order ) {

		if ( ! $order instanceof WC_Order ) {
			$order = wc_get_order( $order_id );
		}

		if ( ! $order ) {
			return;
		}

		foreach ( $order->get_items() as $item ) {
			$gspc_order_item = \GS_Product_Configurator\WC_Order_Item::from( $item );

			foreach ( $gspc_order_item->get_entries() as $entry ) {

				if ( $this->_args['form_id'] && $this->_args['form_id'] != $entry['form_id'] ) {
					continue;
				}

				$form = GFAPI::get_form( $entry['form_id'] );
				$entry_updated = false;

				foreach ( $this->_args['field_map'] as $field_key => $data_key ) {

					$field_id = strpos( $field_key, '.' ) !== false ? 
						explode( '.', $field_key )[0] : 
						$field_key;

					$field = GFFormsModel::get_field( $form, $field_id );
					if ( ! $field ) {
						continue;
					}

					$value = $this->get_value_from_order( $order, $data_key );
					if ( $value !== null ) {
						$entry[ $field_key ] = $value;
						$entry_updated = true;
					}
				}

				if ( $entry_updated ) {
					GFAPI::update_entry( $entry );
				}
			}
		}
	}

	private function get_value_from_order( $order, $data_key ) {
		$order_data = $order->get_data();


		switch ( $data_key ) {
			case 'id':
				return $order->get_id();
			case 'email':
				return $order->get_billing_email();
			case 'status':
				return $order->get_status();
			case 'total':
				return $order->get_total();
		}

		// Nested data (e.g., billing/first_name)
		$parts = explode( '/', $data_key );
		$current = $order_data;

		foreach ( $parts as $part ) {
			if ( isset( $current[ $part ] ) ) {
				$current = $current[ $part ];
			} else {
				return null;
			}
		}

		return $current;
	}
}

# Configuration

new GSPC_Map_Order_Data_to_Entry( array(
	'form_id'    => 123,                 // Replace with your form ID
	'field_map'  => array(
		'2'     => 'id',                 // Field ID 2 will store the order ID
		'3.3'   => 'billing/first_name', // Field ID 3, input 3 (first name)
		'3.6'   => 'billing/last_name',  // Field ID 3, input 6 (last name)
		'4'     => 'email',              // Field ID 4 will store the email
		'5'     => 'total',              // Field ID 5 will store the order total
	),
) );

Comments

  1. Andrei
    Andrei June 3, 2025 at 10:01 am

    For some reason, this snippet is not working any more. (I think there are some changes in the native checkout page of Woocommerce)

    Reply
    1. J Yeager
      J Yeager Staff June 3, 2025 at 11:36 am

      Hey Andrei,

      Sorry to hear that this snippet isn’t working for you anymore. I am sending a follow-up via support so that we can take a look at what’s breaking on your site, and make any appropriate changes.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.