Skip to content

Commit

Permalink
Merge pull request #43 from pronamic/40-fatal-error-on-non-numerical-…
Browse files Browse the repository at this point in the history
…quantity

Add support for `3 stuks` product field quanity
  • Loading branch information
rvdsteege authored May 29, 2024
2 parents ce10c79 + c356de0 commit 325d139
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use GFCommon;
use Pronamic\WordPress\Number\Number;
use Pronamic\WordPress\Number\Parser as NumberParser;
use Pronamic\WordPress\Money\Currency;
use Pronamic\WordPress\Money\Money;
use Pronamic\WordPress\Pay\AbstractGatewayIntegration;
Expand Down Expand Up @@ -356,9 +357,24 @@ public function entry_post_save( $lead, $form ) {
$line->set_unit_price( new Money( $value, $currency ) );

if ( array_key_exists( 'quantity', $product ) ) {
$quantity = Number::from_mixed( $product['quantity'] );

$value = $value->multiply( $quantity );
try {
$parser = new NumberParser();

$quantity = $parser->parse( $product['quantity'] );

$value = $value->multiply( $quantity );
} catch ( \Exception $exception ) {
$exception = new \Exception(
\sprintf(
'Couldn’t parse Gravity Forms product field `%s` quantity to a number.',
\esc_html( $key )
),
0,
$exception
);

throw $exception;
}
}

$line->set_total_amount( new Money( $value, $currency ) );
Expand Down

0 comments on commit 325d139

Please sign in to comment.