1+ <?php
2+ /**
3+ * Gravity Shop // Product Configurator // Map Order Data to Entry
4+ * https://gravitywiz.com/documentation/gravity-shop-product-configurator/
5+ *
6+ * Populate fields that are linked to a WooCommerce product using GS Product Configurator with values from the WooCommerce order.
7+ *
8+ * Instructions Video: https://www.loom.com/share/423b1e3835dc4757aae26a3efe9351b0
9+ *
10+ * Plugin Name: GSPC Map Order Data to Entry
11+ * Plugin URI: https://gravitywiz.com/documentation/gravity-shop-product-configurator/
12+ * Description: Populate fields that are linked to a WooCommerce product using GS Product Configurator with values from the WooCommerce order.
13+ * Author: Gravity Wiz
14+ * Version: 0.1
15+ * Author URI: http://gravitywiz.com
16+ */
17+ class GSPC_Map_Order_Data_to_Entry {
18+
19+ private $ _args = array ();
20+
21+ public function __construct ( $ args = array () ) {
22+
23+ $ this ->_args = wp_parse_args ( $ args , array (
24+ 'form_id ' => false ,
25+ 'field_map ' => array (),
26+ ) );
27+
28+ add_action ( 'init ' , array ( $ this , 'init ' ) );
29+ }
30+
31+ public function init () {
32+
33+ if ( ! property_exists ( 'GFCommon ' , 'version ' ) ||
34+ ! class_exists ( '\GS_Product_Configurator\WC_Order_Item ' ) ||
35+ ! class_exists ( 'GFAPI ' ) ) {
36+ return ;
37+ }
38+
39+ add_action ( 'woocommerce_checkout_order_processed ' , array ( $ this , 'map_order_data_to_entries ' ), 10 , 3 );
40+ }
41+
42+ public function map_order_data_to_entries ( $ order_id , $ posted_data , $ order ) {
43+
44+ if ( ! $ order instanceof WC_Order ) {
45+ $ order = wc_get_order ( $ order_id );
46+ }
47+
48+ if ( ! $ order ) {
49+ return ;
50+ }
51+
52+ foreach ( $ order ->get_items () as $ item ) {
53+ $ gspc_order_item = \GS_Product_Configurator \WC_Order_Item::from ( $ item );
54+
55+ foreach ( $ gspc_order_item ->get_entries () as $ entry ) {
56+
57+ if ( $ this ->_args ['form_id ' ] && $ this ->_args ['form_id ' ] != $ entry ['form_id ' ] ) {
58+ continue ;
59+ }
60+
61+ $ form = GFAPI ::get_form ( $ entry ['form_id ' ] );
62+ $ entry_updated = false ;
63+
64+ foreach ( $ this ->_args ['field_map ' ] as $ field_key => $ data_key ) {
65+
66+ $ field_id = strpos ( $ field_key , '. ' ) !== false ?
67+ explode ( '. ' , $ field_key )[0 ] :
68+ $ field_key ;
69+
70+ $ field = GFFormsModel::get_field ( $ form , $ field_id );
71+ if ( ! $ field ) {
72+ continue ;
73+ }
74+
75+ $ value = $ this ->get_value_from_order ( $ order , $ data_key );
76+ if ( $ value !== null ) {
77+ $ entry [ $ field_key ] = $ value ;
78+ $ entry_updated = true ;
79+ }
80+ }
81+
82+ if ( $ entry_updated ) {
83+ GFAPI ::update_entry ( $ entry );
84+ }
85+ }
86+ }
87+ }
88+
89+ private function get_value_from_order ( $ order , $ data_key ) {
90+ $ order_data = $ order ->get_data ();
91+
92+
93+ switch ( $ data_key ) {
94+ case 'id ' :
95+ return $ order ->get_id ();
96+ case 'email ' :
97+ return $ order ->get_billing_email ();
98+ case 'status ' :
99+ return $ order ->get_status ();
100+ case 'total ' :
101+ return $ order ->get_total ();
102+ }
103+
104+ // Nested data (e.g., billing/first_name)
105+ $ parts = explode ( '/ ' , $ data_key );
106+ $ current = $ order_data ;
107+
108+ foreach ( $ parts as $ part ) {
109+ if ( isset ( $ current [ $ part ] ) ) {
110+ $ current = $ current [ $ part ];
111+ } else {
112+ return null ;
113+ }
114+ }
115+
116+ return $ current ;
117+ }
118+ }
119+
120+ # Configuration
121+
122+ new GSPC_Map_Order_Data_to_Entry ( array (
123+ 'form_id ' => 123 , // Replace with your form ID
124+ 'field_map ' => array (
125+ '2 ' => 'id ' , // Field ID 2 will store the order ID
126+ '3.3 ' => 'billing/first_name ' , // Field ID 3, input 3 (first name)
127+ '3.6 ' => 'billing/last_name ' , // Field ID 3, input 6 (last name)
128+ '4 ' => 'email ' , // Field ID 4 will store the email
129+ '5 ' => 'total ' , // Field ID 5 will store the order total
130+ ),
131+ ) );
0 commit comments