Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active April 10, 2021 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spivurno/da655f24c07e6204a11e2e647113677f to your computer and use it in GitHub Desktop.
Save spivurno/da655f24c07e6204a11e2e647113677f to your computer and use it in GitHub Desktop.
Gravity Perks // GP eCommerce Fields // Deduct Deposit from Order Summary
<?php
/**
* Gravity Perks // GP eCommerce Fields // Deduct Deposit from Order Summary
* http://gravitywiz.com/documentation/gravity-forms-ecommerce-fields/
*/
add_action( 'wp_loaded', function() {
if( ! function_exists( 'gp_ecommerce_fields' ) ) {
return;
}
remove_action( 'gform_product_info', array( gp_ecommerce_fields(), 'add_ecommerce_fields_to_order' ), 9, 3 );
add_action( 'gform_product_info', function( $order, $form, $entry ) {
// CHANGE: Update "123" to the ID of your form.
if( $form['id'] != 123 ) {
return gp_ecommerce_fields()->add_ecommerce_fields_to_order( $order, $form, $entry );
}
// CHANGE: Update the "2" to your deposit field ID.
$deposit =& $order['products'][2];
// Run this first so calculations are reprocessed before we convert deposit to a negative number.
$order = gp_ecommerce_fields()->add_ecommerce_fields_to_order( $order, $form, $entry );
// Convert deposit to a negative number so it is deducted from the total.
$deposit['price'] = GFCommon::to_money( GFCommon::to_number( $deposit['price'], $entry['currency'] ) * $deposit['quantity'] * -1, $entry['currency'] );
// Quantity is factored into price above.
$deposit['quantity'] = 1;
// Set the discount flag so GP eCommerce Fields knows this is a deposit.
$deposit['isDiscount'] = true;
return $order;
}, 9, 3 );
} );
@spivurno
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment