Last active
October 22, 2024 22:08
-
-
Save claygriffiths/4eba8b2f2f1be92f9bd0e19875c7dea8 to your computer and use it in GitHub Desktop.
GS Product Configurator: Populate Field with Order ID
This file contains 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
<?php | |
/** | |
* Gravity Shop // GS Product Configurator // Populate Gravity Forms field with WooCommerce Order ID | |
* | |
* Populate fields in a Gravity Form that is linked to a WooCommerce product using GS Product Configurator | |
* with the WooCommerce order ID after checking out. | |
* | |
* Credit: Rochelle Victor (https://github.com/rochekaid) | |
* | |
* Instructions: | |
* 1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ | |
* 2. Add the CSS class "wc_gf_order_id" to any field in your Gravity Form that you want to populate with the | |
* WooCommerce order ID. | |
*/ | |
add_action( 'woocommerce_checkout_order_processed', function( $order_id ) { | |
if ( ! class_exists( '\GS_Product_Configurator\WC_Order_Item' ) || ! class_exists( 'GFAPI' ) ) { | |
return; | |
} | |
$order = wc_get_order( $order_id ); | |
/** | |
* @var string $field_css_class Any fields with this class will have its value replaced with the | |
* WooCommerce order ID. | |
*/ | |
$field_css_class = 'wc_gf_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 ) { | |
$form = GFAPI::get_form( $entry['form_id'] ); | |
$entry_updated = false; | |
foreach ( $form['fields'] as $field ) { | |
$css_classes = empty( $field->cssClass ) ? [] : explode( ' ', $field->cssClass ); | |
if ( ! in_array( $field_css_class, $css_classes, true ) ) { | |
continue; | |
} | |
$entry[ $field->id ] = $order->ID; | |
$entry_updated = true; | |
} | |
if ( $entry_updated ) { | |
GFAPI::update_entry( $entry ); | |
} | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rochekaid Fixed! Sorry about that. Could've sworn I fixed that 😆