Skip to content

Commit 995198e

Browse files
peterfabianFangedParakeetPanosSynetos
authored
Added filter to update Level 3 data. (#9053)
Co-authored-by: Samir Merchant <[email protected]> Co-authored-by: Panos (Panagiotis Synetos) <[email protected]>
1 parent 889d821 commit 995198e

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

changelog/try-gc-as-discount

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: add
3+
4+
Added filter to enable updating Level 3 data based on order data.

src/Internal/Service/Level3Service.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ public function get_data_from_order( int $order_id ): array {
8585
};
8686
$items_to_send = array_map( $process_item, $order_items );
8787

88-
if ( count( $items_to_send ) > 200 ) {
89-
// If more than 200 items are present, bundle the last ones in a single item.
90-
$items_to_send = array_merge(
91-
array_slice( $items_to_send, 0, 199 ),
92-
[ $this->bundle_level3_data_from_items( array_slice( $items_to_send, 200 ) ) ]
93-
);
94-
}
95-
9688
$level3_data = [
9789
'merchant_reference' => (string) $order->get_id(), // An alphanumeric string of up to characters in length. This unique value is assigned by the merchant to identify the order. Also known as an “Order ID”.
9890
'customer_reference' => (string) $order->get_id(),
@@ -112,6 +104,29 @@ public function get_data_from_order( int $order_id ): array {
112104
$level3_data['shipping_from_zip'] = $store_postcode;
113105
}
114106

107+
/**
108+
* Filters the Level 3 data based on order.
109+
*
110+
* Example usage: Enables updating the discount based on the products in the order,
111+
* if any of the products are gift cards.
112+
*
113+
* @since 8.0.0
114+
*
115+
* @param array $level3_data Precalculated Level 3 data based on order.
116+
* @param WC_Order $order The order object.
117+
*/
118+
$level3_data = apply_filters( 'wcpay_payment_request_level3_data', $level3_data, $order );
119+
120+
if ( count( $level3_data['line_items'] ) > 200 ) {
121+
// If more than 200 items are present, bundle the last ones in a single item.
122+
$items_to_send = array_merge(
123+
array_slice( $level3_data['line_items'], 0, 199 ),
124+
[ $this->bundle_level3_data_from_items( array_slice( $level3_data['line_items'], 199 ) ) ]
125+
);
126+
127+
$level3_data['line_items'] = $items_to_send;
128+
}
129+
115130
return $level3_data;
116131
}
117132

tests/unit/src/Internal/Service/Level3ServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected function mock_level_3_order(
164164

165165
if ( $basket_size > 1 ) {
166166
// Keep the formely created item/fee and add duplicated items to the basket.
167-
$mock_items = array_merge( $mock_items, array_fill( 0, $basket_size - 1, $mock_items[0] ) );
167+
$mock_items = array_merge( $mock_items, array_fill( 0, $basket_size - count( $mock_items ), $mock_items[0] ) );
168168
}
169169

170170
// Setup the order.

0 commit comments

Comments
 (0)