Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2936045784/83304

Summary

Inspired from https://gravitywiz.com/snippet-library/gspc-populate-field-with-order-id/ to support capturing different types of WooCommerce order data (order ID, name, email) with a config instead of class-based. Example:

new GSPC_Map_Order_Data_to_Entry( array( 
    'form_id' => 123,
    'field_map' => array(
        1   => 'id',
        2.3 => 'billing/first_name',
        2.6 => 'billing/last_name',
        3   => 'email', 
    ) 
) );

Setup and Walkthrough Loom:
https://www.loom.com/share/423b1e3835dc4757aae26a3efe9351b0

Copy link

coderabbitai bot commented May 15, 2025

Walkthrough

A new PHP class, GSPC_Map_Order_Data_to_Entry, is introduced to map WooCommerce order data to Gravity Forms entries associated with the Gravity Shop Product Configurator. The class hooks into the WooCommerce checkout process, retrieves relevant order and entry data, maps specified fields, and updates Gravity Forms entries accordingly.

Changes

File(s) Change Summary
gs-product-configurator/gspc-map-order-data-to-entry.php Added class GSPC_Map_Order_Data_to_Entry to map WooCommerce order data to Gravity Forms entries.

Sequence Diagram(s)

sequenceDiagram
    participant Customer
    participant WooCommerce
    participant GSPC_Map_Order_Data_to_Entry
    participant GS_Product_Configurator
    participant Gravity_Forms

    Customer->>WooCommerce: Complete checkout
    WooCommerce->>GSPC_Map_Order_Data_to_Entry: Trigger woocommerce_checkout_order_processed
    GSPC_Map_Order_Data_to_Entry->>GS_Product_Configurator: Get Gravity Forms entries for order items
    GSPC_Map_Order_Data_to_Entry->>Gravity_Forms: Retrieve form and field objects
    GSPC_Map_Order_Data_to_Entry->>WooCommerce: Fetch mapped order data values
    GSPC_Map_Order_Data_to_Entry->>Gravity_Forms: Update entry fields with order data
    Gravity_Forms-->>GSPC_Map_Order_Data_to_Entry: Confirm entry update
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 338e025

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
gs-product-configurator/gspc-map-order-data-to-entry.php (3)

52-63: Consider adding logging for debugging purposes.

While the code correctly processes order items and entries, consider adding optional logging capability to help with troubleshooting when entries aren't being updated as expected.

 				if ( $this->_args['form_id'] && $this->_args['form_id'] != $entry['form_id'] ) {
+					// Optional debug logging could be added here
 					continue;
 				}

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

89-102: Consider extending special case handling with more order properties.

The method handles common order properties but could be extended to support more properties directly rather than relying on nested data access. This would make the API more intuitive for users.

 	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();
+			case 'date_created':
+				return $order->get_date_created()->format('Y-m-d H:i:s');
+			case 'payment_method':
+				return $order->get_payment_method_title();
+			case 'currency':
+				return $order->get_currency();
 		}

120-131: Provide more context around the example configuration.

The example configuration is helpful but could benefit from additional context about needing to be customized. Also, it would be beneficial to ensure users understand this code needs to be modified and is currently active.

 # Configuration
 
-new GSPC_Map_Order_Data_to_Entry( array(
+/**
+ * Example configuration - IMPORTANT: Modify with your own form ID and field mappings
+ * Remove or comment out this instantiation if you're not ready to use it yet.
+ */
+$gspc_order_mapping = 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
 	),
 ) );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between efab86e and 338e025.

📒 Files selected for processing (1)
  • gs-product-configurator/gspc-map-order-data-to-entry.php (1 hunks)
🔇 Additional comments (5)
gs-product-configurator/gspc-map-order-data-to-entry.php (5)

17-29: Well-structured class initialization with proper hooks.

The class is well-organized with constructor parameters and default values. The use of wp_parse_args() for merging defaults with provided arguments is a good practice.


31-40: Good dependency checking before proceeding with initialization.

The code properly verifies the existence of required classes before adding hooks, which prevents potential errors if dependencies are missing.


42-51: Robust order validation.

The method appropriately verifies that we have a valid WC_Order object before proceeding with any operations.


64-80: Field key parsing logic looks good.

The code correctly handles both simple field IDs and complex inputs with dot notation. The null check before updating entries prevents adding empty values.


104-117: Robust nested data retrieval implementation.

The nested data retrieval logic is well-implemented, with proper null checks to prevent PHP notices or warnings when accessing non-existent data paths.

Comment on lines +1 to +118
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant