Creating a summary cart table for product purchases in Gravity Forms

Use this snippet to show the users product selections, quantity and prices in Gravity Forms

Creating a summary cart table for product purchases in Gravity Forms
Shawn Wernig
June 19, 2024
by Shawn Wernig

Recently I had a client who wanted a registration form for their upcoming conference where the guest could select from a bunch of add-on options. At the end of the form, right before the guest is prompted to make their purchase, Gravity Forms allows for only a simple 'total' field. We felt that this was insufficient, so I created a neat little plugin that will summarize all of the Products in your form, and output the guests choices for them to review.

Here it is! Include this as a new file in your theme or plugin.

IMPORTANT: This plugin works ONLY if you’re using pagination on your form. If you are not, simply add pagination in such a way that the final page of your form is your ‘cart review’ page, and make sure you include a Total field, and a payment option field.


if (class_exists('GF_Field'))
{

    class GF_Cart extends GF_Field {

        public $type = 'cart_table';

        public function get_form_editor_field_title() {
            return "Cart Table";
        }

        public function get_form_editor_button() {
            return [
                'group' => 'pricing_fields',
                'text'  => $this->get_form_editor_field_title(),
            ];
        }

        public function get_form_editor_field_settings() {
            return [
                'label_setting',
                'css_class_setting',
                'conditional_logic_field_setting'
            ];
        }

        public function get_field_input( $form, $value = '', $entry = null) {


            if ($this->is_form_editor()) return '<p>Cart Table</p>';


            ob_start();

            ?>
            <table class="table">
                <thead>
                <tr>
                    <td>Item</td>
                    <td>Price</td>
                    <td>Qty</td>
                    <td>Total</td>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach( $form['fields'] as $field )
                {
                    /**
                     *
                     * Here we need some info - what has the customer purchased?
                     *
                     */
                    $selectedValue = [
                        'price' => 0,
                        'qty' => 0
                    ];
                    $formatter = new \NumberFormatter("en-US", \NumberFormatter::CURRENCY );
                    $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 2);

                    // if not a product, continue
                    if( $field->type != 'product' ) continue;

                    // skip if this field was not set.
                    if(  ! ( isset( $_POST['input_' . $field->id ]) ||  isset( $_POST['input_' . $field->id . "_1"]) ) ) continue;

                    $selectionLabel = false;
                    switch( $field->inputType  )
                    {
                        case 'select':
                        case 'radio':

                            $value = $_POST['input_' . $field->id ];
                            $value = explode('|', $value );
                            $selectionLabel = $value[0];
                            $selectedValue['price'] = $value[1];
                            $selectedValue['qty'] = 1;
                            break;
                        case 'calculation':
                        case 'singleproduct':
                            $priceString = $_POST['input_' . $field->id .'_2' ];
                            preg_match('/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/', $priceString, $matches);

                            $selectedValue['price'] = ( count( $matches ) > 0 )
                                ? array_shift( $matches )
                                : 0;

                            $selectedValue['qty'] = $_POST['input_' . $field->id .'_3' ];

                        break;

                    }

                    ?>
                    <tr id="product-id-<?php echo $field->id; ?>" data-id="<?php echo $field->id; ?>">
                        <th>
                            <?php echo $field->label; ?>
                            <?php echo $selectionLabel ? " - {$selectionLabel}" : ''; ?>
                        </th>
                        <td>
                            <?php
                            echo $formatter->formatCurrency($selectedValue['price'], $formatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL) );
                            ?>
                        </td>
                        <td>
                            <?php
                            if( $field->disableQuantity )
                            {
                                echo '1';
                            }
                            else
                            {
                                echo $selectedValue['qty'] ? $selectedValue['qty'] : 'None';
                            }
                            ?>
                        </td>
                        <td>
                            <?php
                            $priceToDisplay = ( $field->disableQuantity )
                                ?  $selectedValue['price']
                                : floatval( $selectedValue['price'] ) * intval( $selectedValue['qty'] );

                            if( $priceToDisplay > 0 )
                            {
                                echo $formatter->formatCurrency( $priceToDisplay, $formatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL) );
                            }
                            else
                            {
                                echo '--';
                            }

                            ?>

                        </td>
                    </tr>
                    <?php

                }
                ?>
                </tbody>
            </table>

            <?php
            return ob_get_clean();
        }
    }


    GF_Fields::register( new GF_Cart() );
}

Eggplant Studios - Custom Website Design and Development
Creating custom web solutions and happy clients since 2002
COPYRIGHT ©
2024
Eggplant Studios
- ALL RIGHTS RESERVED
LOGIN