为此,您需要通过为重力表单创建自定义属性来将重力表单字段与 woocommerce 结帐表单字段映射。
以下只是您可以实现上述功能的步骤和挂钩示例。
add_action( 'gform_field_standard_settings', 'gf_add_woo_field' , 10, 2 );
add_action( 'gform_editor_js','gf_editor_script' , 11, 2 );
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function gf_add_woo_field( $position, $form_id ) {
$woo_fields = WC()->checkout()->checkout_fields;
for retrieving fields
if ( $position == 1368 ) {?>
<li class="woo_checkout_field_type_setting field_setting">
<label for="woo_checkout_field_type" class="section_label">
<?php esc_html_e( 'Field Type', 'gravityforms' ); ?>
<?php gform_tooltip( 'form_field_type' ) ?>
</label>
<select id="woo_checkout_field_type" onchange="if(jQuery(this).val() == '') return; jQuery('#field_settings').slideUp(function(){StartChangeWooFieldType(jQuery('#woo_checkout_field_type').val());});">
foreach ($woo_fields as key=>val){ ?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php }
}
?>
function gf_editor_script() {
?>
<script type='text/javascript'>
jQuery(document).bind("gform_load_field_settings",
function(event, field, form){
//follow the code for attribute Field Type in post custom field gravity form
});
</script>
<?php }
function set_post_content( $entry, $form ) {
if ( ! empty( $form['fields'] ) && ! empty( $entry ) ) {
foreach ( $form['fields'] as $field ) {
if ( $field->type == '' ) {
//find the attribute match and save in usermeta from entry creator email id or user id
$field_id = $field->id;
$value = $entry[ $field_id ];
update($user_id,'billing_address',$value);
}
}
}
}
注意:不要粘贴相同的代码。您必须根据自己的要求修改其中的某些部分。
您可以搜索 existing 插件并使用它。
关注:https://wisdmlabs.com/blog/add-custom-attribute-to-gravity-forms-field/