The snippet allows you to automatically populate WooCommerce billing and shipping fields as well as order comments based on form fields attached to a product in Gravity Forms.
Step 1: Install the PHP Snippet
- Access Code Snippets Plugin:
- Log in to your WordPress dashboard.
- Navigate to Plugins > Add New.
- Search for and install the Code Snippets plugin if it’s not already installed.
- Activate the plugin.
- Add the Snippet:
- Go to Snippets > Add New in your WordPress dashboard.
- Give the snippet a meaningful name, such as
Map GF Fields to WooCommerce Checkout. - Paste the PHP code provided in the snippet documentation.
- Save and Activate:
- Click Save Changes and Activate.
- Ensure the snippet is active for it to take effect.
add_filter(‘woocommerce_checkout_get_value’, function( $input, $key ) {
// Ensure WooCommerce cart is initialized before proceeding
if ( ! WC()->cart ) {
return $input;
}
// Get current items in the cart.
$cart_items = WC()->cart->get_cart();
// Loop through cart items.
foreach ( $cart_items as $cart_item ) {
// Convert to a GSPC Cart Item
$gspc_cart_item = \GS_Product_Configurator\WC_Cart_Item::from( $cart_item );
// Get the feed for the cart item.
$feed = $gspc_cart_item->get_feed();
if ( ! $feed ) {
continue;
}
// Get the entry for the cart item.
$entry = $gspc_cart_item->get_entry();
if ( ! $entry ) {
continue;
}
// Map the fields to the checkout fields. The first cart item that has values is the one we use.
$field_map = gs_product_configurator()->get_field_map_fields( $feed, ‘checkoutFieldMap’ );
if ( empty( $field_map ) || ! is_array( $field_map ) ) {
continue;
}
$values = array();
foreach ( $field_map as $name => $input_id ) {
if ( empty( $input_id ) || empty( $entry[ $input_id ] ) ) {
continue;
}
// If the field is a country input in address field, convert it to abbreviation.
$field = GFAPI::get_field( $feed[‘form_id’], $input_id );
$input_bits = explode( ‘.’, $input_id );
if ( $field && $field->type === ‘address’ && count( $input_bits ) == 2 && $input_bits[1] == 6 ) {
$values[ $name ] = gspc_get_country_abbreviation( $entry[ $input_id ] );
continue;
}
$values[ $name ] = $entry[ $input_id ];
}
if ( empty( array_filter( $values ) ) ) {
continue;
}
foreach ( $values as $checkout_key => $value ) {
if ( $checkout_key === $key ) {
$input = $value;
break;
}
}
}
return $input;
}, 10, 2);
Step 2: Configure the GS Product Configurator Feed
- Access the GS Product Configurator Feed Settings:
- In your WordPress dashboard, go to Forms > Select Form > Settings > Product Configurator.
- Enable Checkout Field Mapping:
- Locate the Checkout Field Map section in the feed settings.
- Map each Gravity Forms field to the corresponding WooCommerce checkout field (e.g., Billing Name, Shipping Address, Order Notes).
- Save the feed settings.
Step 3: Test the Integration
- Add a Product to the Cart:
- Go to the product page on your website that uses the configured Gravity Form.
- Fill out the form and add the product to the cart.
- Proceed to Checkout:
- Navigate to the WooCommerce checkout page.
- Verify that the mapped fields are auto-filled with the data from the Gravity Form.
- Adjust as Needed:
- If any fields are not mapping correctly, return to the Checkout Field Map section in the GS Product Configurator feed settings and adjust the mappings.
Change Log
Version 1.1
Date: 27 December, 2024
Changes:
- Bug Fix: Added a check to verify if the
WC()->cartobject is initialized before accessing it.- Reason: Prevented a critical error occurring in the Theme Customizer, where WooCommerce cart objects are not available.
- Impact: Ensures compatibility with the Theme Customizer and other contexts where the WooCommerce cart is unavailable.