Skip to content

Commit d0faf48

Browse files
authored
gspc-map-order-data-to-entry.php: Added snippet to map order data to entry.
1 parent e1cbc66 commit d0faf48

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
if ( empty( $this->_args['form_id'] ) ) {
29+
error_log( 'GSPC_Map_Order_Data_to_Entry requires a form_id.' );
30+
return;
31+
}
32+
33+
add_action( 'init', array( $this, 'init' ) );
34+
}
35+
36+
public function init() {
37+
38+
if ( ! property_exists( 'GFCommon', 'version' ) ||
39+
! class_exists( '\GS_Product_Configurator\WC_Order_Item' ) ||
40+
! class_exists( 'GFAPI' ) ) {
41+
return;
42+
}
43+
44+
add_action( 'woocommerce_checkout_order_processed', array( $this, 'map_order_data_to_entries' ), 10, 3 );
45+
}
46+
47+
public function map_order_data_to_entries( $order_id, $posted_data, $order ) {
48+
49+
if ( ! $order instanceof WC_Order ) {
50+
$order = wc_get_order( $order_id );
51+
}
52+
53+
if ( ! $order ) {
54+
return;
55+
}
56+
57+
foreach ( $order->get_items() as $item ) {
58+
$gspc_order_item = \GS_Product_Configurator\WC_Order_Item::from( $item );
59+
60+
foreach ( $gspc_order_item->get_entries() as $entry ) {
61+
62+
if ( $this->_args['form_id'] && $this->_args['form_id'] != $entry['form_id'] ) {
63+
continue;
64+
}
65+
66+
$form = GFAPI::get_form( $entry['form_id'] );
67+
$entry_updated = false;
68+
69+
foreach ( $this->_args['field_map'] as $field_key => $data_key ) {
70+
71+
$field_id = strpos( $field_key, '.' ) !== false ?
72+
explode( '.', $field_key )[0] :
73+
$field_key;
74+
75+
$field = GFFormsModel::get_field( $form, $field_id );
76+
if ( ! $field ) {
77+
continue;
78+
}
79+
80+
$value = $this->get_value_from_order( $order, $data_key );
81+
if ( $value !== null ) {
82+
$entry[ $field_key ] = $value;
83+
$entry_updated = true;
84+
}
85+
}
86+
87+
if ( $entry_updated ) {
88+
GFAPI::update_entry( $entry );
89+
}
90+
}
91+
}
92+
}
93+
94+
private function get_value_from_order( $order, $data_key ) {
95+
$order_data = $order->get_data();
96+
97+
98+
switch ( $data_key ) {
99+
case 'id':
100+
return $order->get_id();
101+
case 'email':
102+
return $order->get_billing_email();
103+
case 'status':
104+
return $order->get_status();
105+
case 'total':
106+
return $order->get_total();
107+
}
108+
109+
// Nested data (e.g., billing/first_name)
110+
$parts = explode( '/', $data_key );
111+
$current = $order_data;
112+
113+
foreach ( $parts as $part ) {
114+
if ( isset( $current[ $part ] ) ) {
115+
$current = $current[ $part ];
116+
} else {
117+
return null;
118+
}
119+
}
120+
121+
return $current;
122+
}
123+
}
124+
125+
# Configuration
126+
127+
new GSPC_Map_Order_Data_to_Entry( array(
128+
'form_id' => 123, // Replace with your form ID
129+
'field_map' => array(
130+
'2' => 'id', // Field ID 2 will store the order ID
131+
'3.3' => 'billing/first_name', // Field ID 3, input 3 (first name)
132+
'3.6' => 'billing/last_name', // Field ID 3, input 6 (last name)
133+
'4' => 'email', // Field ID 4 will store the email
134+
'5' => 'total', // Field ID 5 will store the order total
135+
),
136+
) );

0 commit comments

Comments
 (0)