Created
August 4, 2023 21:45
-
-
Save claygriffiths/971ddc5b33051b6eb13f121c789ef75c to your computer and use it in GitHub Desktop.
GS Product Configurator: Map form fields to checkout
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 | |
add_filter('woocommerce_checkout_get_value', function( $input, $key ) { | |
// 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 = []; | |
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); | |
function gspc_get_country_abbreviation( $country ) { | |
$fake_address_field = new GF_Field_Address(); | |
if ( method_exists( $fake_address_field, 'get_default_countries' ) ) { | |
/** | |
* @var array Associative array of countries. Keys are abbreviations for the country. | |
*/ | |
$countries = $fake_address_field->get_default_countries(); | |
} else { | |
$countries = array(); | |
} | |
foreach ( $countries as $abbreviation => $country_name ) { | |
if ( $country === $country_name || $country === $abbreviation ) { | |
return $abbreviation; | |
} | |
} | |
return ''; | |
} | |
// Add a new section and field map to map the fields to the checkout fields. | |
add_filter( 'gform_gs-product-configurator_feed_settings_fields', function( $feed_settings_fields, $gspc ) { | |
$feed_settings_fields[] = [ | |
'title' => esc_html__( 'Checkout Field Map', 'gs-product-configurator' ), | |
'fields' => [ | |
[ | |
'name' => 'checkoutFieldMap', | |
'type' => 'field_map', | |
'key_field' => [ | |
'title' => 'Product Field', | |
], | |
'value_field' => [ | |
'title' => 'Checkout Field', | |
], | |
'field_map' => [ | |
[ | |
'name' => 'billing_first_name', | |
'label' => esc_html__( 'Billing First Name', 'gs-product-configurator' ), | |
'field_type' => [ 'text', 'name' ], | |
], | |
[ | |
'name' => 'billing_last_name', | |
'label' => esc_html__( 'Billing Last Name', 'gs-product-configurator' ), | |
'field_type' => [ 'text', 'name' ], | |
], | |
[ | |
'name' => 'billing_company', | |
'label' => esc_html__( 'Billing Company', 'gs-product-configurator' ), | |
'field_type' => [ 'text' ], | |
], | |
[ | |
'name' => 'billing_address_1', | |
'label' => esc_html__( 'Billing Address Line 1', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'billing_address_2', | |
'label' => esc_html__( 'Billing Address Line 2', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'billing_city', | |
'label' => esc_html__( 'Billing City', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'billing_state', | |
'label' => esc_html__( 'Billing State', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'billing_postcode', | |
'label' => esc_html__( 'Billing ZIP/Postal Code', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'billing_phone', | |
'label' => esc_html__( 'Billing Phone', 'gs-product-configurator' ), | |
'field_type' => [ 'phone' ], | |
], | |
[ | |
'name' => 'billing_email', | |
'label' => esc_html__( 'Billing Email', 'gs-product-configurator' ), | |
'field_type' => [ 'email' ], | |
], | |
[ | |
'name' => 'billing_country', | |
'label' => esc_html__( 'Billing Country', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_first_name', | |
'label' => esc_html__( 'Shipping First Name', 'gs-product-configurator' ), | |
'field_type' => [ 'text', 'name' ], | |
], | |
[ | |
'name' => 'shipping_last_name', | |
'label' => esc_html__( 'Shipping Last Name', 'gs-product-configurator' ), | |
'field_type' => [ 'text', 'name' ], | |
], | |
[ | |
'name' => 'shipping_company', | |
'label' => esc_html__( 'Shipping Company', 'gs-product-configurator' ), | |
'field_type' => [ 'text' ], | |
], | |
[ | |
'name' => 'shipping_address_1', | |
'label' => esc_html__( 'Shipping Address Line 1', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_address_2', | |
'label' => esc_html__( 'Shipping Address Line 2', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_city', | |
'label' => esc_html__( 'Shipping City', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_state', | |
'label' => esc_html__( 'Shipping State', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_postcode', | |
'label' => esc_html__( 'Shipping ZIP/Postal Code', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'shipping_country', | |
'label' => esc_html__( 'Shipping Country', 'gs-product-configurator' ), | |
'field_type' => [ 'address' ], | |
], | |
[ | |
'name' => 'order_comments', | |
'label' => esc_html__( 'Order Comments', 'gs-product-configurator' ), | |
], | |
], | |
], | |
], | |
]; | |
return $feed_settings_fields; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment