-
Notifications
You must be signed in to change notification settings - Fork 90
gspc-map-order-data-to-entry.php
: Added snippet to map order data to entry.
#1106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
saifsultanc
merged 2 commits into
master
from
saif/add/83304-add-snippet-map-order-data
May 31, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
gs-product-configurator/gspc-map-order-data-to-entry.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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: http://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 | ||
), | ||
) ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.