0
votes

I'm using Gravity Forms and WooCommerce as well as the WooCommerce Gravity Forms Addons plugins. I have a form attached to a product which needs to do some calculations to determine the ultimate price for the product.

In order to do so, I have some intermediate calculational fields in the form which I don't want to show up in the cart, or in the entry for the order.

I've already reviewed and tried gform_pre_submission, and I can successfully remove the desired fields. The problem is that Gravity Forms apparently recalculates the form on submission and so unsetting the fields in gform_pre_submission breaks the calculations and causes the item added to the cart to have an incorrect value.

Obviously I can hide the fields in the cart with CSS, but that doesn't keep the necessary fields out of the entry and thus in the WooCommerce order information.

So, how can I omit the undesired fields from the entry, without breaking the calculations?

Thanks!

PS - Here's the presubmission code I tried in case there's an issue with my test

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

}

Edit: See David's code below. I made this one modification to it to deal with oddball products:

        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] );
            }
        }

A screenshot of the $other_data value for a product with BOTH WooCommerce variations AND Gravity Forms Addons variations:

$other_data var dump

2

2 Answers

1
votes

This snippet (can be installed as a plugin) adds an option to each field to allow you to hide it from the WooCommerce cart item description (screenshot).

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

1
votes

For ease of those finding this later, here's @David's handy code with my modification to support products with both WooCommerce variations and Gravity Forms Addons variations.

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