Skip to content

Commit 486454d

Browse files
fix: phpunit
1 parent 6cfc70a commit 486454d

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

inc/functions.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,22 +1582,26 @@ function ppom_extract_matrix_by_quantity( $quantities_field, $product, $quantity
15821582
return $matrix;
15831583
}
15841584

1585-
$matrix_index = 0;
1586-
$quantity = intval( $quantity );
1585+
$quantity = intval( $quantity );
15871586
foreach ( $ranges as $range => $data ) {
15881587

1589-
$range_array = explode( '-', $range );
1590-
$range_start = $range_array[0];
1591-
$range_end = isset( $range_array[1] ) ? $range_array[1] : 0;
1588+
if ( strpos( $range, '-' ) !== false ) {
1589+
list( $start, $end ) = array_map( 'intval', explode( '-', $range ) );
1590+
if ( $quantity >= $start && $quantity <= $end ) {
1591+
$matrix = $data;
1592+
break;
1593+
}
1594+
} else {
1595+
$value = intval( $range );
15921596

1593-
if ( 0 === $matrix_index && $quantity < intval( $range_start ) && 0 === $range_end ) {
1594-
$matrix = $data;
1595-
break;
1596-
} elseif ( 0 !== $matrix_index && $quantity >= $range_start && ( $quantity <= $range_end || $range_end == 0 ) ) {
1597-
$matrix = $data;
1598-
break;
1597+
if ( $range === array_key_first( $ranges ) && $quantity < $value ) {
1598+
$matrix = $data;
1599+
break;
1600+
} elseif ( $range === array_key_last( $ranges ) && $quantity >= $value ) {
1601+
$matrix = $data;
1602+
break;
1603+
}
15991604
}
1600-
++$matrix_index;
16011605
}
16021606

16031607
return $matrix;

tests/unit/test-checkout-lifecycle.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,19 @@ public function testWooCommerceGetCartItemFromSessionRecalculatesMatrixPriceWhen
262262
'price_matrix',
263263
array(
264264
array(
265-
'option' => '1-2',
265+
'option' => '5',
266266
'price' => '12',
267267
'label' => 'Low quantity',
268268
'id' => 'low_qty',
269269
),
270270
array(
271-
'option' => '3-5',
271+
'option' => '5-10',
272+
'price' => '10',
273+
'label' => 'Range quantity',
274+
'id' => 'range_qty',
275+
),
276+
array(
277+
'option' => '11',
272278
'price' => '8',
273279
'label' => 'High quantity',
274280
'id' => 'high_qty',
@@ -279,11 +285,13 @@ public function testWooCommerceGetCartItemFromSessionRecalculatesMatrixPriceWhen
279285
$product->get_id()
280286
);
281287

282-
$initial = $this->restore_cart_item_from_session( wc_get_product( $product->get_id() ), array(), 1 );
283-
$updated = $this->restore_cart_item_from_session( wc_get_product( $product->get_id() ), array(), 3 );
288+
$low_quantity = $this->restore_cart_item_from_session( wc_get_product( $product->get_id() ), array(), 3 );
289+
$range_quantity = $this->restore_cart_item_from_session( wc_get_product( $product->get_id() ), array(), 7 );
290+
$high_quantity = $this->restore_cart_item_from_session( wc_get_product( $product->get_id() ), array(), 15 );
284291

285-
$this->assertSame( 12.0, (float) $initial['data']->get_price() );
286-
$this->assertSame( 8.0, (float) $updated['data']->get_price() );
292+
$this->assertSame( 12.0, (float) $low_quantity['data']->get_price() );
293+
$this->assertSame( 10.0, (float) $range_quantity['data']->get_price() );
294+
$this->assertSame( 8.0, (float) $high_quantity['data']->get_price() );
287295
}
288296

289297
/**

0 commit comments

Comments
 (0)