Skip to content

Commit

Permalink
Added filter to update Level 3 data. (#9053)
Browse files Browse the repository at this point in the history
Co-authored-by: Samir Merchant <[email protected]>
Co-authored-by: Panos (Panagiotis Synetos) <[email protected]>
  • Loading branch information
3 people authored and lovo-h committed Aug 1, 2024
1 parent b9ad05d commit 2ecf159
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions changelog/try-gc-as-discount
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: add

Added filter to enable updating Level 3 data based on order data.
31 changes: 23 additions & 8 deletions src/Internal/Service/Level3Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ public function get_data_from_order( int $order_id ): array {
};
$items_to_send = array_map( $process_item, $order_items );

if ( count( $items_to_send ) > 200 ) {
// If more than 200 items are present, bundle the last ones in a single item.
$items_to_send = array_merge(
array_slice( $items_to_send, 0, 199 ),
[ $this->bundle_level3_data_from_items( array_slice( $items_to_send, 200 ) ) ]
);
}

$level3_data = [
'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”.
'customer_reference' => (string) $order->get_id(),
Expand All @@ -112,6 +104,29 @@ public function get_data_from_order( int $order_id ): array {
$level3_data['shipping_from_zip'] = $store_postcode;
}

/**
* Filters the Level 3 data based on order.
*
* Example usage: Enables updating the discount based on the products in the order,
* if any of the products are gift cards.
*
* @since 8.0.0
*
* @param array $level3_data Precalculated Level 3 data based on order.
* @param WC_Order $order The order object.
*/
$level3_data = apply_filters( 'wcpay_payment_request_level3_data', $level3_data, $order );

if ( count( $level3_data['line_items'] ) > 200 ) {
// If more than 200 items are present, bundle the last ones in a single item.
$items_to_send = array_merge(
array_slice( $level3_data['line_items'], 0, 199 ),
[ $this->bundle_level3_data_from_items( array_slice( $level3_data['line_items'], 199 ) ) ]
);

$level3_data['line_items'] = $items_to_send;
}

return $level3_data;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/Internal/Service/Level3ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected function mock_level_3_order(

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

// Setup the order.
Expand Down

0 comments on commit 2ecf159

Please sign in to comment.