Php 重力表单-从条目中忽略字段
我正在使用重力表单和WooCommerce以及WooCommerce重力表单插件。我有一个附在产品上的表格,需要做一些计算来确定产品的最终价格 为了做到这一点,我在表单中有一些中间计算字段,我不想在购物车或订单条目中显示这些字段 我已经检查并尝试了gform_pre_提交,我可以成功删除所需的字段。问题在于重力表单显然会在提交时重新计算表单,因此在gform_pre_submission中取消设置字段会破坏计算,并导致添加到购物车中的项目的值不正确 显然,我可以用CSS隐藏购物车中的字段,但这不会将必要的字段保留在条目之外,从而保留在订单信息中 那么,如何在不破坏计算的情况下从条目中省略不需要的字段呢 谢谢 PS-这是我试过的预提交代码,以防我的测试出现问题Php 重力表单-从条目中忽略字段,php,wordpress,gravity-forms-plugin,gravityforms,Php,Wordpress,Gravity Forms Plugin,Gravityforms,我正在使用重力表单和WooCommerce以及WooCommerce重力表单插件。我有一个附在产品上的表格,需要做一些计算来确定产品的最终价格 为了做到这一点,我在表单中有一些中间计算字段,我不想在购物车或订单条目中显示这些字段 我已经检查并尝试了gform_pre_提交,我可以成功删除所需的字段。问题在于重力表单显然会在提交时重新计算表单,因此在gform_pre_submission中取消设置字段会破坏计算,并导致添加到购物车中的项目的值不正确 显然,我可以用CSS隐藏购物车中的字段,但这不
add_action( 'gform_pre_submission_5', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
//remove some fields which we don't need to save
unset($_POST['input_23']); //remove base price
}
编辑:参见下面的David代码。我对其进行了一次修改,以处理古怪的产品:
for( $i = count( $other_data ) - 1; $i >= 0; $i-- ) {
if (isset($other_data[$i]['name'])){ //if not, must be a WC variation, not GF so ignore
if( $other_data[$i]['name'] == GFCommon::get_label( $field ) )
unset( $other_data[$i] );
}
}
WooCommerce变体和Gravity Forms插件变体产品的$other_数据值屏幕截图:
此代码段(可以作为插件安装)为每个字段添加了一个选项,允许您在WooCommerce购物车项目描述()中隐藏它
此代码段(可以作为插件安装)为每个字段添加了一个选项,允许您在WooCommerce购物车项目描述()中隐藏它
为了便于以后查找,这里是@David的便捷代码,我对其进行了修改,以支持具有WooCommerce变体和Gravity Forms插件变体的产品
/**
* WooCommerce Gravity Forms Add-on: Add support for removing a field from the product description in the cart.
* This handy code came from: https://gist.github.com/spivurno/6951662 as a result of this
* discussion: https://gravitywiz.com/how-to-hide-gravity-forms-product-fields/
*
* It's used to remove the calulational fields in product forms from the cart.
*/
class WooGFRemoveFieldFromProductDescription {
public function __construct() {
add_action( 'gform_field_advanced_settings', array( $this, 'field_settings_ui' ), 10, 2 );
add_action( 'gform_editor_js', array( $this, 'field_settings_js' ) );
add_filter( 'woocommerce_get_item_data', array( $this, 'modify_item_data' ), 11, 2 );
add_action( 'woocommerce_add_order_item_meta', array( $this, 'delete_order_item_meta' ), 11, 2 );
}
public function modify_item_data( $other_data, $cart_item ) {
$form_id = rgars( $cart_item, '_gravity_form_data/id' );
if( ! $form_id )
return $other_data;
$form = GFFormsModel::get_form_meta( $form_id );
foreach( $form['fields'] as $field ) {
if( ! rgar( $field, 'wgfrfEnable' ) )
continue;
//var_dump($other_data); //debug
//echo '<br>';
// reindex array for next loop
$other_data = array_values( $other_data );
for( $i = count( $other_data ) - 1; $i >= 0; $i-- ) {
if (isset($other_data[$i]['name'])){ //if not, must be a WC variation, not GF so ignore
if( $other_data[$i]['name'] == GFCommon::get_label( $field ) )
unset( $other_data[$i] );
}
}
}
return $other_data;
}
public function delete_order_item_meta( $item_id, $cart_item ) {
$form_id = rgars( $cart_item, '_gravity_form_data/id' );
if( ! $form_id )
return;
$form = GFFormsModel::get_form_meta( $form_id );
foreach( $form['fields'] as $field ) {
if( ! rgar( $field, 'wgfrfEnable' ) )
continue;
woocommerce_delete_order_item_meta( $item_id, GFCommon::get_label( $field ) );
}
}
public function field_settings_ui( $position ) {
if( $position != 450 )
return;
?>
<li class="wgfrf-enable-setting field_setting">
<input type="checkbox" id="wgfrf-enable" value="1" onclick="SetFieldProperty( 'wgfrfEnable', this.checked )">
<label class="inline" for="wgfrf-enable">
<?php _e( 'Remove This Field From WooCommerce Cart Item Description' ); ?>
</label>
</li>
<?php
}
public function field_settings_js() {
?>
<script type="text/javascript">
(function($) {
$(document).bind('gform_load_field_settings', function(event, field, form) {
$("#wgfrf-enable").attr( 'checked', field.wgfrfEnable == true );
});
for( inputType in fieldSettings ) {
if( fieldSettings.hasOwnProperty( inputType ) )
fieldSettings[inputType] += ', .wgfrf-enable-setting';
}
})(jQuery);
</script>
<?php
}
}
new WooGFRemoveFieldFromProductDescription();
/**
*WooCommerce表单加载项:添加对从购物车中的产品描述中删除字段的支持。
*这个方便的代码来自:https://gist.github.com/spivurno/6951662 因此,
*讨论:https://gravitywiz.com/how-to-hide-gravity-forms-product-fields/
*
*它用于从购物车中删除产品表单中的计算字段。
*/
类WooGFRemoveFieldFromProductDescription{
公共函数构造(){
添加操作('gform\u field\u advanced\u settings',array($this,'field\u settings\u ui'),10,2);
添加动作('gform_editor_js',数组($this,'field_settings_js'));
添加_过滤器('woocommerce_get_item_data',数组($this,'modify_item_data'),11,2);
add_action('woocommerce_add_order_item_meta',数组('this,'delete_order_item_meta'),11,2);
}
公共函数修改商品数据($other商品数据,$cart商品){
$form_id=rgars($cart_item,'u gravity_form_data/id');
如果(!$form\u id)
返回$other_数据;
$form=GFFormsModel::get\u form\u meta($form\u id);
foreach($form['fields']作为$field){
如果(!rgar($field,'wgfrferfenable'))
继续;
//var_dump($other_data);//调试
//回声“
”;
//为下一个循环重新索引数组
$other_data=数组_值($other_data);
对于($i=count($other_data)-1;$i>=0;$i--){
if(isset($other_data[$i]['name']){//如果不是,则必须是WC变体,而不是GF,因此忽略
if($other_data[$i]['name']==GFCommon::get_label($field))
未设置($其他_数据[$i]);
}
}
}
返回$other_数据;
}
公共函数delete\u order\u item\u meta($item\u id,$cart\u item){
$form_id=rgars($cart_item,'u gravity_form_data/id');
如果(!$form\u id)
返回;
$form=GFFormsModel::get\u form\u meta($form\u id);
foreach($form['fields']作为$field){
如果(!rgar($field,'wgfrferfenable'))
继续;
woocommerce_delete_order_item_meta($item_id,GFCommon::get_label($field));
}
}
公共功能字段\设置\用户界面($position){
如果($position!=450)
返回;
?>
(函数($){
$(文档).bind('gform\u load\u field\u settings',函数(事件、字段、表单){
$(“#wgfrf enable”).attr('checked',field.wgfrfEnable==true);
});
用于(在字段设置中输入类型){
if(fieldSettings.hasOwnProperty(inputType))
字段设置[inputType]+=',.wgfrfrf启用设置';
}
})(jQuery);
为了便于以后发现,这里有@David的方便代码和我的修改,以支持WooCommerce变体和Gravity Forms插件变体的产品
/**
* WooCommerce Gravity Forms Add-on: Add support for removing a field from the product description in the cart.
* This handy code came from: https://gist.github.com/spivurno/6951662 as a result of this
* discussion: https://gravitywiz.com/how-to-hide-gravity-forms-product-fields/
*
* It's used to remove the calulational fields in product forms from the cart.
*/
class WooGFRemoveFieldFromProductDescription {
public function __construct() {
add_action( 'gform_field_advanced_settings', array( $this, 'field_settings_ui' ), 10, 2 );
add_action( 'gform_editor_js', array( $this, 'field_settings_js' ) );
add_filter( 'woocommerce_get_item_data', array( $this, 'modify_item_data' ), 11, 2 );
add_action( 'woocommerce_add_order_item_meta', array( $this, 'delete_order_item_meta' ), 11, 2 );
}
public function modify_item_data( $other_data, $cart_item ) {
$form_id = rgars( $cart_item, '_gravity_form_data/id' );
if( ! $form_id )
return $other_data;
$form = GFFormsModel::get_form_meta( $form_id );
foreach( $form['fields'] as $field ) {
if( ! rgar( $field, 'wgfrfEnable' ) )
continue;
//var_dump($other_data); //debug
//echo '<br>';
// reindex array for next loop
$other_data = array_values( $other_data );
for( $i = count( $other_data ) - 1; $i >= 0; $i-- ) {
if (isset($other_data[$i]['name'])){ //if not, must be a WC variation, not GF so ignore
if( $other_data[$i]['name'] == GFCommon::get_label( $field ) )
unset( $other_data[$i] );
}
}
}
return $other_data;
}
public function delete_order_item_meta( $item_id, $cart_item ) {
$form_id = rgars( $cart_item, '_gravity_form_data/id' );
if( ! $form_id )
return;
$form = GFFormsModel::get_form_meta( $form_id );
foreach( $form['fields'] as $field ) {
if( ! rgar( $field, 'wgfrfEnable' ) )
continue;
woocommerce_delete_order_item_meta( $item_id, GFCommon::get_label( $field ) );
}
}
public function field_settings_ui( $position ) {
if( $position != 450 )
return;
?>
<li class="wgfrf-enable-setting field_setting">
<input type="checkbox" id="wgfrf-enable" value="1" onclick="SetFieldProperty( 'wgfrfEnable', this.checked )">
<label class="inline" for="wgfrf-enable">
<?php _e( 'Remove This Field From WooCommerce Cart Item Description' ); ?>
</label>
</li>
<?php
}
public function field_settings_js() {
?>
<script type="text/javascript">
(function($) {
$(document).bind('gform_load_field_settings', function(event, field, form) {
$("#wgfrf-enable").attr( 'checked', field.wgfrfEnable == true );
});
for( inputType in fieldSettings ) {
if( fieldSettings.hasOwnProperty( inputType ) )
fieldSettings[inputType] += ', .wgfrf-enable-setting';
}
})(jQuery);
</script>
<?php
}
}
new WooGFRemoveFieldFromProductDescription();
/**
*WooCommerce表单加载项:添加对从购物车中的产品描述中删除字段的支持。
*这个方便的代码来自:https://gist.github.com/spivurno/6951662 因此,
*讨论:https://gravitywiz.com/how-to-hide-gravity-forms-product-fields/
*
*它用于从购物车中删除产品表单中的计算字段。
*/
类WooGFRemoveFieldFromProductDescription{
公共函数构造(){
添加操作('gform\u field\u advanced\u settings',array($this,'field\u settings\u ui'),10,2);
添加动作('gform_editor_js',数组($this,'field_settings_js'));
添加_过滤器('woocommerce_get_item_data',数组($this,'modify_item_data'),11,2);
add_action('woocommerce_add_order_item_meta',数组('this,'delete_order_item_meta'),11,2);
}
公共函数修改商品数据($other商品数据,$cart商品){
$form\u id=rgars($cart\u item,'.\u gravity\u用于