Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gw-quantity-as-decimal.php: Added suppport for decimals in feed product quantity. #835

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions gravity-forms/gw-quantity-as-decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ function init() {

if ( $this->global ) {
add_filter( 'gform_field_validation', array( $this, 'allow_quantity_float' ), 10, 4 );
add_filter( 'gform_submission_data_pre_process_payment', array( $this, 'modify_submission_data' ), 10, 4 );
} else {
add_filter( 'gform_field_validation_' . $this->form_id, array( $this, 'allow_quantity_float' ), 10, 4 );
add_filter( 'gform_submission_data_pre_process_payment_' . $this->form_id, array( $this, 'modify_submission_data' ), 10, 4 );

}

if ( GFFormsModel::is_html5_enabled() ) {
Expand Down Expand Up @@ -90,6 +93,114 @@ function modify_quantity_input_tag( $markup, $field, $value, $lead_id, $form_id
return $markup;
}

function modify_submission_data( $submission_data, $feed, $form, $entry ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is pretty hefty, and I'm admittedly having a hard time following what exactly we're doing here.

The appears to be a copy and paste of get_order_data with some changes. Is that correct? If so, can you call that out and note what changes you made so if something changes upstream that breaks this snippet, we'll know what to update in the future?


$products = GFCommon::get_product_fields( $form, $entry );

$key = rgars( $feed, 'meta/transactionType' ) === 'subscription' ? 'recurringAmount' : 'paymentAmount';
$payment_field = rgars( $feed, 'meta/' . $key, 'form_total' );
$setup_fee_field = rgar( $feed['meta'], 'setupFee_enabled' ) ? $feed['meta']['setupFee_product'] : false;
$trial_field = rgar( $feed['meta'], 'trial_enabled' ) ? rgars( $feed, 'meta/trial_product' ) : false;

$amount = 0;
$line_items = array();
$discounts = array();
$fee_amount = 0;
$trial_amount = 0;
foreach ( $products['products'] as $field_id => $product ) {

$quantity = isset( $product['quantity'] ) ? $product['quantity'] : 1;
$product_price = GFCommon::to_number( $product['price'], $entry['currency'] );

$options = array();
if ( is_array( rgar( $product, 'options' ) ) ) {
foreach ( $product['options'] as $option ) {
$options[] = $option['option_name'];
$product_price += $option['price'];
}
}

$is_trial_or_setup_fee = false;

if ( ! empty( $trial_field ) && $trial_field == $field_id ) {

$trial_amount = $product_price * $quantity;
$is_trial_or_setup_fee = true;

} elseif ( ! empty( $setup_fee_field ) && $setup_fee_field == $field_id ) {

$fee_amount = $product_price * $quantity;
$is_trial_or_setup_fee = true;
}

// Do not add to line items if the payment field selected in the feed is not the current field.
if ( is_numeric( $payment_field ) && $payment_field != $field_id ) {
continue;
}

// Do not add to line items if the payment field is set to "Form Total" and the current field was used for trial or setup fee.
if ( $is_trial_or_setup_fee && ! is_numeric( $payment_field ) ) {
continue;
}

$amount += $product_price * $quantity;

$description = '';
if ( ! empty( $options ) ) {
$description = esc_html__( 'options: ', 'gravityforms' ) . ' ' . implode( ', ', $options );
}

if ( $product_price >= 0 ) {
$line_items[] = array(
'id' => $field_id,
'name' => $product['name'],
'description' => $description,
'quantity' => $quantity,
'unit_price' => GFCommon::to_number( $product_price, $entry['currency'] ),
'options' => rgar( $product, 'options' ),
);
} else {
$discounts[] = array(
'id' => $field_id,
'name' => $product['name'],
'description' => $description,
'quantity' => $quantity,
'unit_price' => GFCommon::to_number( $product_price, $entry['currency'] ),
'options' => rgar( $product, 'options' ),
);
}
}

if ( $trial_field == 'enter_amount' ) {
$trial_amount = rgar( $feed['meta'], 'trial_amount' ) ? GFCommon::to_number( rgar( $feed['meta'], 'trial_amount' ), $entry['currency'] ) : 0;
}

if ( ! empty( $products['shipping']['name'] ) && ! is_numeric( $payment_field ) ) {
$line_items[] = array(
'id' => $products['shipping']['id'],
'name' => $products['shipping']['name'],
'description' => '',
'quantity' => 1,
'unit_price' => GFCommon::to_number( $products['shipping']['price'], $entry['currency'] ),
'is_shipping' => 1,
);
$amount += $products['shipping']['price'];
}

// Round amount to resolve floating point precision issues.
$currency = RGCurrency::get_currency( $entry['currency'] );
$decimals = rgar( $currency, 'decimals', 0 );
$amount = GFCommon::round_number( $amount, $decimals );

$submission_data['payment_amount'] = $amount;
$submission_data['setup_fee'] = $fee_amount;
$submission_data['trial'] = $trial_amount;
$submission_data['line_items'] = $line_items;
$submission_data['discounts'] = $discounts;

return $submission_data;
}

function get_field_input( $field, $value, $form ) {

remove_filter( 'gform_field_input', array( $this, 'modify_quantity_input_tag' ), 10, 5 );
Expand Down
Loading