【问题标题】:Gravity Forms - Omit fields from entry重力形式 - 从条目中省略字段
【发布时间】:2018-07-23 03:17:36
【问题描述】:

我正在使用 Gravity Forms 和 WooCommerce 以及 WooCommerce Gravity Forms Addons 插件。我有一个附加到产品的表格,需要进行一些计算以确定产品的最终价格。

为此,我在表单中有一些中间计算字段,我不想显示在购物车或订单条目中。

我已经查看并尝试了 gform_pre_submission,我可以成功删除所需的字段。问题是 Gravity Forms 显然会在提交时重新计算表单,因此取消设置 gform_pre_submission 中的字段会破坏计算并导致添加到购物车的项目具有不正确的值。

显然我可以使用 CSS 隐藏购物车中的字段,但这不会将必要的字段保留在条目之外,因此不会保留在 WooCommerce 订单信息中。

那么,我怎样才能在不破坏计算的情况下从条目中省略不需要的字段?

谢谢!

PS - 这是我尝试的预提交代码,以防我的测试出现问题

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

}

编辑:见下面大卫的代码。我对其进行了一次修改以处理古怪的产品:

        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_data 值的屏幕截图:

$other_data var dump

【问题讨论】:

    标签: php wordpress gravity-forms-plugin gravityforms


    【解决方案1】:

    为了方便那些以后发现这一点,这里是 @David 的方便代码和我的修改,以支持具有 WooCommerce 变体和 Gravity Forms Addons 变体的产品。

    /**
     * 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();
    

    【讨论】:

      【解决方案2】:

      这个 sn-p(可以作为插件安装)为每个字段添加一个选项,允许您将其隐藏在 WooCommerce 购物车项目描述中 (screenshot)。

      https://gist.github.com/spivurno/6951662

      【讨论】:

      • 我实际上是在发布此代码后找到了您的代码,发现它运行良好,但我不得不进行一次修改。如果您的产品仅使用 Gravity Forms Addon,那么没有问题。我虽然有一些混蛋产品,它们有 WooCommerce Variations 和 GF 插件,显然 WC 在一些相对较新的更新中改变了 $other_data 的格式。 WC 现在正在使用键/值对,这会破坏代码中的 ['name'] 测试。我在上面的原始问题中添加了我的编辑。
      猜你喜欢
      • 2015-10-25
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多