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

Added filter to update Level 3 data. #9053

Merged
merged 12 commits into from
Jul 23, 2024
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
Loading