mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Add filters woocommerce_paypal_payments_cart_line_item_name and woocommerce_paypal_payments_order_line_item_name.
This commit is contained in:
parent
895cc486d4
commit
b8eed0f324
5 changed files with 84 additions and 2 deletions
|
@ -40,6 +40,21 @@ 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;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -202,6 +202,11 @@ class WebhookEndpoint {
|
|||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 204 !== $status_code ) {
|
||||
$json = null;
|
||||
/**
|
||||
* Use in array as consistency check.
|
||||
*
|
||||
* @psalm-suppress RedundantConditionGivenDocblockType
|
||||
*/
|
||||
if ( is_array( $response ) ) {
|
||||
$json = json_decode( $response['body'] );
|
||||
}
|
||||
|
|
|
@ -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'] ) ) {
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,6 +101,11 @@ class PaymentCaptureRefunded implements RequestHandler {
|
|||
)
|
||||
);
|
||||
if ( is_wp_error( $refund ) ) {
|
||||
/**
|
||||
* Helps to asset type.
|
||||
*
|
||||
* @psalm-suppress RedundantCondition
|
||||
*/
|
||||
assert( $refund instanceof WP_Error );
|
||||
$message = sprintf(
|
||||
'Order %1$s could not be refunded. %2$s',
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue