Skip to content

Commit

Permalink
Added gpcp-product-pricing-labels.js and gpcp-product-pricing-labels.…
Browse files Browse the repository at this point in the history
…php (#18)

* Added gpcp-product-pricing-labels.js and gpcp-product-pricing-labels.php

Original gist: https://gist.github.com/spivurno/cc461abccceaf9aec531210cb0846476

* Added var in front of variables and replaced EOL commas with semicolons
  • Loading branch information
scyt committed Dec 7, 2020
1 parent fe835bf commit 6deea2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gp-conditional-pricing/gpcp-product-pricing-labels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Gravity Perks // GP Conditional Pricing // Display Price Labels (JS)
* http://gravitywiz.com/documentation/gravity-forms-conditional-pricing/
*
* This snippet requires the PHP counterpart gpcp-product-pricing-labels.php
*/
( function( $ ) {
function update_price_labels() {
$( 'label[data-gpcp-template], option[data-gpcp-template]' ).each( function() {
var $priceElem = $( this ).is( 'option' ) ? $( this ) : $( this ).siblings( 'input' );
var price = gformFormatMoney( $priceElem.val().split( '|' )[1] );
var template = $( this ).attr( 'data-gpcp-template' );
$( this ).html( template.replace( '{price}', price ) );
} );
}
gform.addAction( 'gpcp_after_update_pricing', update_price_labels);
update_price_labels();
} )( jQuery );
21 changes: 21 additions & 0 deletions gp-conditional-pricing/gpcp-product-pricing-labels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Gravity Perks // GP Conditional Pricing // Display Price Labels (PHP)
* http://gravitywiz.com/documentation/gravity-forms-conditional-pricing/
*
* This snippet requires the JS counterpart gpcp-product-pricing-labels.js
*/
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 );

0 comments on commit 6deea2a

Please sign in to comment.