Created
December 12, 2023 11:51
-
-
Save InpsydeNiklas/cce603f8bae8d2193b67305d1c671164 to your computer and use it in GitHub Desktop.
WooCommerce PayPal Payments - Copy buyer email address to Memo field in PayPal order (Memo field may not always be present)
This file contains 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 to get the email from WooCommerce Checkout or Order | |
function get_wc_checkout_email() { | |
if ( $order_id = WC()->session->get('order_awaiting_payment') ) { | |
$order = wc_get_order($order_id); | |
return $order->get_billing_email(); | |
} else { | |
return WC()->checkout()->get_value('billing_email'); | |
} | |
} | |
// Modify the create order data | |
add_filter('ppcp_create_order_request_body_data', static function (array $data): array { | |
if (isset($data['purchase_units'][0]['items'][0]['description'])) { | |
$email = get_wc_checkout_email(); | |
$data['purchase_units'][0]['items'][0]['description'] = $email; | |
} | |
return $data; | |
}); | |
// Modify the patch order data | |
add_filter('ppcp_patch_order_request_body_data', function($patches_array) { | |
if (isset($patches_array[0]['value']['items'][0])) { | |
$email = get_wc_checkout_email(); | |
$patches_array[0]['value']['items'][0]['description'] = $email; | |
} | |
return $patches_array; | |
}, 10); |
@beinhd The buyer email address can be displayed below the products like this:
Keep in mind this may be visible to buyers in their PayPal confirmation mails. But to achieve it you can use this code snippet:
/**
* Retrieves the email address from the WooCommerce Checkout or Order.
*
* This function checks if there's an order awaiting payment to get the billing email.
* If not, it fetches the email from the checkout form data.
*
* @return string The billing email address.
*/
function get_wc_checkout_email() {
if ($order_id = WC()->session->get('order_awaiting_payment')) {
$order = wc_get_order($order_id);
return $order->get_billing_email();
} else {
return WC()->checkout()->get_value('billing_email');
}
}
/**
* Modifies the PayPal order payload to include an additional line item representing the buyer's email.
*
* The function adds a line item with the buyer's email as a product with a zero price.
* It ensures that the currency code of this item matches that of other items in the order.
*
* @param array $patches_array The original payload for modifying a PayPal order.
* @return array The modified payload with the additional line item.
*/
function customize_paypal_order($patches_array) {
$email = get_wc_checkout_email();
// Default currency code, used if no other items are found from which to infer the currency.
$currency_code = 'USD';
// If items exist in the payload, use the currency code of the first item.
if (isset($patches_array[0]['value']['items']) && !empty($patches_array[0]['value']['items'])) {
$currency_code = $patches_array[0]['value']['items'][0]['unit_amount']['currency_code'];
}
// Define the line item for the email.
$email_item = array(
'name' => $email,
'quantity' => 1,
'sku' => 'Buyer email', // SKU for internal tracking
'unit_amount' => array(
'currency_code' => $currency_code,
'value' => '0.00', // Zero price to ensure it doesn't affect the order total
),
);
// Append the new email item to the existing list of items.
if (isset($patches_array[0]['value']['items'])) {
$patches_array[0]['value']['items'][] = $email_item;
} else {
$patches_array[0]['value']['items'] = array($email_item);
}
return $patches_array;
}
// Register the function to modify PayPal order data just before it's sent.
add_filter('ppcp_patch_order_request_body_data', 'customize_paypal_order', 10);
If you need more guidance customizing the plugin to your needs, I recommend getting in touch with the support team. Hope this helps!
thank you
hi sir i sell iptv any option to add it to woocommerce So that PayPal does not know what I am selling
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to Copy buyer email address to Order details field in PayPal order