Merge pull request #1601 from woocommerce/PCP-881-compatibility-with-third-party-product-add-ons-plugins

Compatibility with third-party "Product Add-Ons" plugins (881)
This commit is contained in:
Emili Castells 2023-09-07 16:45:26 +02:00 committed by GitHub
commit 53f82b74f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 3 deletions

View file

@ -43,6 +43,20 @@ class ApiModule implements ModuleInterface {
WC()->session->set( 'ppcp_fees', $fees );
}
);
add_filter(
'ppcp_create_order_request_body_data',
function( array $data ) use ( $c ) {
foreach ( $data['purchase_units'] as $purchase_unit_index => $purchase_unit ) {
foreach ( $purchase_unit['items'] as $item_index => $item ) {
$data['purchase_units'][ $purchase_unit_index ]['items'][ $item_index ]['name'] =
apply_filters( 'woocommerce_paypal_payments_cart_line_item_name', $item['name'], $item['cart_item_key'] );
}
}
return $data;
}
);
add_action(
'woocommerce_paypal_payments_paypal_order_created',
function ( Order $order ) use ( $c ) {

View file

@ -202,7 +202,15 @@ class WebhookEndpoint {
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( 204 !== $status_code ) {
$json = json_decode( $response['body'] ) ?? null;
$json = null;
/**
* Use in array as consistency check.
*
* @psalm-suppress RedundantConditionGivenDocblockType
*/
if ( is_array( $response ) ) {
$json = json_decode( $response['body'] );
}
throw new PayPalApiException(
$json,
$status_code

View file

@ -116,6 +116,13 @@ class OrderProcessor {
*/
private $order_helper;
/**
* Array to store temporary order data changes to restore after processing.
*
* @var array
*/
private $restore_order_data = array();
/**
* OrderProcessor constructor.
*
@ -292,8 +299,12 @@ class OrderProcessor {
* @return Order
*/
public function patch_order( \WC_Order $wc_order, Order $order ): Order {
$this->apply_outbound_order_filters( $wc_order );
$updated_order = $this->order_factory->from_wc_order( $wc_order, $order );
$order = $this->order_endpoint->patch_order_with( $order, $updated_order );
$this->restore_order_from_filters( $wc_order );
$order = $this->order_endpoint->patch_order_with( $order, $updated_order );
return $order;
}
@ -323,4 +334,48 @@ class OrderProcessor {
true
);
}
/**
* Applies filters to the WC_Order, so they are reflected only on PayPal Order.
*
* @param WC_Order $wc_order The WoocOmmerce Order.
* @return void
*/
private function apply_outbound_order_filters( WC_Order $wc_order ): void {
$items = $wc_order->get_items();
$this->restore_order_data['names'] = array();
foreach ( $items as $item ) {
if ( ! $item instanceof \WC_Order_Item ) {
continue;
}
$original_name = $item->get_name();
$new_name = apply_filters( 'woocommerce_paypal_payments_order_line_item_name', $original_name, $item->get_id(), $wc_order->get_id() );
if ( $new_name !== $original_name ) {
$this->restore_order_data['names'][ $item->get_id() ] = $original_name;
$item->set_name( $new_name );
}
}
}
/**
* Restores the WC_Order to it's state before filters.
*
* @param WC_Order $wc_order The WooCommerce Order.
* @return void
*/
private function restore_order_from_filters( WC_Order $wc_order ): void {
if ( is_array( $this->restore_order_data['names'] ?? null ) ) {
foreach ( $this->restore_order_data['names'] as $wc_item_id => $original_name ) {
$wc_item = $wc_order->get_item( $wc_item_id, false );
if ( $wc_item ) {
$wc_item->set_name( $original_name );
}
}
}
}
}

View file

@ -57,6 +57,7 @@ class OrderProcessorTest extends TestCase
->andReturn($payments);
$wcOrder = Mockery::mock(\WC_Order::class);
$wcOrder->expects('get_items')->andReturn([]);
$wcOrder->expects('update_meta_data')
->with(PayPalGateway::ORDER_PAYMENT_MODE_META_KEY, 'live');
$wcOrder->shouldReceive('get_id')->andReturn(1);
@ -193,7 +194,8 @@ class OrderProcessorTest extends TestCase
->andReturn($payments);
$wcOrder = Mockery::mock(\WC_Order::class);
$orderStatus = Mockery::mock(OrderStatus::class);
$wcOrder->expects('get_items')->andReturn([]);
$orderStatus = Mockery::mock(OrderStatus::class);
$orderStatus
->shouldReceive('is')
->with(OrderStatus::APPROVED)