Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions gs-product-configurator/gspc-map-order-data-to-entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?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(),
) );

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling for GFAPI::update_entry failures.

The code currently doesn't handle potential errors when updating Gravity Forms entries. Consider adding error handling to catch and log any failures during the update process.

 				if ( $entry_updated ) {
-					GFAPI::update_entry( $entry );
+					$update_result = GFAPI::update_entry( $entry );
+					if ( is_wp_error( $update_result ) ) {
+						// Handle or log the error
+						error_log( sprintf( 'Failed to update entry %d: %s', $entry['id'], $update_result->get_error_message() ) );
+					}
 				}
🤖 Prompt for AI Agents
In gs-product-configurator/gspc-map-order-data-to-entry.php around lines 80 to
110, the call to GFAPI::update_entry does not handle potential errors. Modify
the code to check the return value of GFAPI::update_entry, which returns a
WP_Error on failure, and if an error occurs, log the error details using an
appropriate logging mechanism or error_log. This will ensure that any failures
during entry updates are captured and can be diagnosed.


# 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
),
) );
Loading