spivurno
2/25/2017 - 2:21 AM

Gravity Perks // GP Conditional Pricing // Filter Entries by Conditionally Priced Products

Gravity Perks // GP Conditional Pricing // Filter Entries by Conditionally Priced Products

<?php
/**
 * Gravity Perks // GP Conditional Pricing // Filter Entries by Conditionally Priced Products
 * http://gravitywiz.com/documentation/gravity-forms-conditional-pricing/
 */
add_filter( 'gform_form_post_get_meta', function( $form ) {

    if( GFForms::get_page() != 'entry_list' ) {
        return $form;
    }

	$pricing_logic     = GP_Conditional_Pricing::get_pricing_logic( $form );
	$pricing_input_ids = array_keys( $pricing_logic );
	$pricing_field_ids = array_map( 'intval', $pricing_input_ids );

    foreach( $form['fields'] as &$field ) {

	    if( $field['type'] != 'product' || ! in_array( $field['id'], $pricing_field_ids ) || empty( $field->choices ) ) {
		    continue;
	    }

	    $choices = $field->choices;
	    $new_choices = array();

	    foreach( $pricing_logic as $product_id => $pricing_levels ) {

	        if( intval( $product_id ) != $field->id ) {
	            continue;
            }

            $is_input_specific = intval( $product_id ) != $product_id;

            foreach( $pricing_levels as $pricing_level ) {
	            $choice_number = 1;
                foreach( $choices as $choice ) {
	                if( $is_input_specific ) {
	                    list( $field_id, $input_index ) = explode( '.', $product_id );
	                    if( $choice_number % 10 == 0 ) {
	                        $choice_number++;
	                    }
                        if( $choice_number == $input_index ) {
                            $new_choices[] = array(
	                            'text'  => sprintf( '%s (%s)', $choice['text'], GFCommon::to_money( $pricing_level['price'] ) ),
                                'value' => $choice['value'],
                                'price' => $pricing_level['price']
                            );
		                }
		                $choice_number++;
	                } else {
		                $new_choices[] = array(
			                'text'  => sprintf( '%s (%s)', $choice['text'], GFCommon::to_money( $pricing_level['price'] ) ),
			                'value' => $choice['value'],
			                'price' => $pricing_level['price']
		                );
                    }
                }
            }

        }


        $field->choices = array_merge( $choices, $new_choices );

    }

    return $form;
} );