Last active
April 25, 2023 07:55
-
-
Save InpsydeNiklas/63e783fd16e664a5f2a31c276d2ad5d3 to your computer and use it in GitHub Desktop.
Modify PayPal Payments order number during order creation and order patching with custom get_my_invoice_number() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function customize_paypal_invoice_number( $data ) { | |
// Access the global WooCommerce object | |
global $woocommerce; | |
// Get the current order | |
$order = $woocommerce->session->get('order_awaiting_payment') ? | |
wc_get_order($woocommerce->session->get('order_awaiting_payment')) : null; | |
// Check if the order object exists | |
if ( $order ) { | |
// Get the custom invoice number using the provided function | |
$custom_invoice_number = get_my_invoice_number( $order ); | |
// Check if the purchase_units are available in the data array | |
if ( isset( $data['purchase_units'][0] ) ) { | |
// Set the custom invoice number | |
$data['purchase_units'][0]['invoice_id'] = $custom_invoice_number; | |
} | |
} | |
return $data; | |
} | |
add_filter( 'ppcp_create_order_request_body_data', 'customize_paypal_invoice_number', 10, 1 ); | |
function customize_paypal_patch_invoice_number( $patches_array ) { | |
// Access the global WooCommerce object | |
global $woocommerce; | |
// Get the current order | |
$order = $woocommerce->session->get('order_awaiting_payment') ? | |
wc_get_order($woocommerce->session->get('order_awaiting_payment')) : null; | |
// Check if the order object exists | |
if ( $order ) { | |
// Get the custom invoice number using the provided function | |
$custom_invoice_number = get_my_invoice_number( $order ); | |
// Add a new patch operation to set the custom invoice number | |
$patches_array[] = array( | |
'op' => 'add', | |
'path' => '/purchase_units/@reference_id==\'default\'/invoice_id', | |
'value' => $custom_invoice_number, | |
); | |
} | |
return $patches_array; | |
} | |
add_filter( 'ppcp_patch_order_request_body_data', 'customize_paypal_patch_invoice_number', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment