从 Gravity Form 覆盖 Woocommerce 账单字段
Override Woocommerce billing fields from Gravity Form
我正在尝试使用商店每个产品上设置的重力表单中的输入字段值覆盖计费和运输默认 woocommerce 值。
我可以通过执行 gform_after_submission_1 钩子来获取重力表单字段值。我的疑问是如何覆盖帐单字段。
我的代码逻辑尝试如下工作:
//Hook action when selects payment and submits order
add_action("gform_after_submission_1", "after_submission", 20, 3);
function after_submission($entry, $form, $fields){
/*Here is where I want to call woocommerce_checkout_fields filter
passing $entry form field input values*/
}
// Override checkout fields
add_filter("woocommerce_checkout_fields" , "custom_override_checkout_fields", 10, 1);
//And here is where I override billing values
function custom_override_checkout_fields($entry) {
$fields['billing']['billing_first_name']['default'] = $entry['14.3'];
return $fields;
}
为此,您需要通过为重力表单创建自定义属性,将重力表单字段映射到 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 add-on 并使用它。
关注:https://wisdmlabs.com/blog/add-custom-attribute-to-gravity-forms-field/
终于,我找到了解决办法。
首先,我需要找到正确的钩子来覆盖订单账单和送货字段。所以,我在 woocommerce_checkout_create_order
hook.
里面做了一些魔法
add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
function override_checkout_fields( $order, $data ){
//Do your magic here
}
然后我在添加到购物车后找到了 Gravity Form 字段 'stored' 的位置,最后,从 'captured' 设置账单和运输字段。
代码看起来像这样:
add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
function override_checkout_fields( $order, $data ){
$cart = WC()->cart->get_cart();
$field_name_billing = $values['_gravity_form_lead']['17.3']
$field_name_shipping = $values['_gravity_form_lead']['17.3']
//... rest of fields
$order->set_billing_field_name($field_name_billing);
$order->set_shipping_field_name($field_name_shipping);
}
我正在尝试使用商店每个产品上设置的重力表单中的输入字段值覆盖计费和运输默认 woocommerce 值。
我可以通过执行 gform_after_submission_1 钩子来获取重力表单字段值。我的疑问是如何覆盖帐单字段。
我的代码逻辑尝试如下工作:
//Hook action when selects payment and submits order
add_action("gform_after_submission_1", "after_submission", 20, 3);
function after_submission($entry, $form, $fields){
/*Here is where I want to call woocommerce_checkout_fields filter
passing $entry form field input values*/
}
// Override checkout fields
add_filter("woocommerce_checkout_fields" , "custom_override_checkout_fields", 10, 1);
//And here is where I override billing values
function custom_override_checkout_fields($entry) {
$fields['billing']['billing_first_name']['default'] = $entry['14.3'];
return $fields;
}
为此,您需要通过为重力表单创建自定义属性,将重力表单字段映射到 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 add-on 并使用它。
关注:https://wisdmlabs.com/blog/add-custom-attribute-to-gravity-forms-field/
终于,我找到了解决办法。
首先,我需要找到正确的钩子来覆盖订单账单和送货字段。所以,我在 woocommerce_checkout_create_order
hook.
add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
function override_checkout_fields( $order, $data ){
//Do your magic here
}
然后我在添加到购物车后找到了 Gravity Form 字段 'stored' 的位置,最后,从 'captured' 设置账单和运输字段。
代码看起来像这样:
add_action( 'woocommerce_checkout_create_order', 'override_checkout_fields', 10, 2 );
function override_checkout_fields( $order, $data ){
$cart = WC()->cart->get_cart();
$field_name_billing = $values['_gravity_form_lead']['17.3']
$field_name_shipping = $values['_gravity_form_lead']['17.3']
//... rest of fields
$order->set_billing_field_name($field_name_billing);
$order->set_shipping_field_name($field_name_shipping);
}