spivurno
10/27/2017 - 1:24 PM

Gravity Perks // GP Conditional Pricing // Display Price Labels

Gravity Perks // GP Conditional Pricing // Display Price Labels

<script>
gform.addAction( 'gpcp_after_update_pricing', function( triggerFieldId, gpcp ) {
  ( function( $ ) {
    $( 'label[data-gpcp-template], option[data-gpcp-template]' ).each( function() {
		var $priceElem = $( this ).is( 'option' ) ? $( this ) : $( this ).siblings( 'input' ),
			price = gformFormatMoney( $priceElem.val().split( '|' )[1] ),
			template = $( this ).attr( 'data-gpcp-template' );
		$( this ).html( template.replace( '{price}', price ) );
    } );
  } )( jQuery );
} );
</script>
<?php
/**
 * Gravity Perks // GP Conditional Pricing // Display Price Labels
 * http://gravitywiz.com/documentation/gravity-forms-conditional-pricing/
 */
add_filter( 'gform_field_choice_markup_pre_render', function ( $choice_markup, $choice, $field, $value ) {

	// Edit the template here. Use {label} to represent the original choice label and {price} where ever you would like to include the choice price.
	$template = '{label} - {price}';

	// No need to edit below.
	if ( $field->type === 'product' ) {
		$template = str_replace( '{label}', $choice['text'], $template );
		$replace = sprintf( ' data-gpcp-template="%s">%s<', $template, str_replace( '{price}', GFCommon::to_money( $choice['price'] ), $template ) );
		$choice_markup = str_replace( ">{$choice['text']}<", $replace, $choice_markup );;
	}

	return $choice_markup;
}, 10, 4 );