Skip to content

Instantly share code, notes, and snippets.

@InpsydeNiklas
InpsydeNiklas / functions.php
Created December 12, 2023 11:51
WooCommerce PayPal Payments - Copy buyer email address to Memo field in PayPal order (Memo field may not always be present)
<?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');
}
@InpsydeNiklas
InpsydeNiklas / functions.php
Last active July 23, 2024 17:03
PayPal Payments - set empty strings as product description in API request
<?php
add_filter('ppcp_create_order_request_body_data', static function (array $data): array {
if (isset($data['purchase_units'][0]['items'][0]['description'])) {
$data['purchase_units'][0]['items'][0]['description'] = '';
}
return $data;
});
@InpsydeNiklas
InpsydeNiklas / functions.php
Created June 9, 2023 09:19
PayPal Payments - unset SKU, empty description, and replace the product title with "Order #123"
<?php
add_filter('ppcp_create_order_request_body_data', function (array $data): array {
foreach ($data['purchase_units'] as $index => $purchase_unit) {
// extract order number from custom_id
$order_number = isset($purchase_unit['custom_id']) ? $purchase_unit['custom_id'] : 'N/A';
foreach ($purchase_unit['items'] as $item_index => $item) {
$data['purchase_units'][$index]['items'][$item_index]['description'] = '';
unset($data['purchase_units'][$index]['items'][$item_index]['sku']);
@InpsydeNiklas
InpsydeNiklas / functions.php
Last active June 2, 2023 07:46
Hide the PayPal gateway and smart buttons on Checkout page for countries other than ['US', 'CA', 'PR']
<?php
// PHP function to unset Stripe gateway
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_countries' );
function payment_gateway_disable_countries( $gateways ) {
if ( is_admin() ) {
return $gateways;
}
if ( ! is_object( WC()->customer ) OR ! method_exists( WC()->customer, 'get_billing_country' ) ) {
@InpsydeNiklas
InpsydeNiklas / functions.php
Last active April 25, 2023 07:55
Modify PayPal Payments order number during order creation and order patching with custom get_my_invoice_number() function
<?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;
@InpsydeNiklas
InpsydeNiklas / functions.php
Last active April 19, 2023 09:43
Remove PayPal Payments from "change_payment_method" page for WooCommerce Subscription
<?php
function remove_paypal_payment_gateways_from_change_payment_endpoint( $available_gateways ) {
if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['change_payment_method'] ) ) {
unset( $available_gateways['ppcp-gateway'] );
unset( $available_gateways['ppcp-credit-card-gateway'] );
}
@InpsydeNiklas
InpsydeNiklas / functions.php
Created March 27, 2023 16:02
(Multisite) Customize WooCommerce order number by adding a prefix based on the current blog ID
<?php
/**
* // functions.php
* Customize order number by adding a prefix based on the current blog ID
*/
add_filter('woocommerce_order_number', 'multisite_change_woocommerce_order_number', 1, 2);
function multisite_change_woocommerce_order_number($order_id, $order) {
$blog_id = get_current_blog_id();
@InpsydeNiklas
InpsydeNiklas / functions.php
Created February 3, 2023 18:13
Add "What is PayPal?" text to PayPal Payments gateway title
<?php
function add_ppcp_gateway_title_link( $title, $id ) {
if ( 'ppcp-gateway' === $id ) {
$title .= sprintf( ' <a href="%s" class="about_ppcp" onclick="javascript:window.open(\'%s\',\'WIPPcp\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700\'); return false;" title="What is PayPal?">What is PayPal?</a>', esc_url( 'https://www.paypal.com/us/webapps/mpp/paypal-popup' ), esc_url( 'https://www.paypal.com/us/webapps/mpp/paypal-popup' ) );
}
return $title;
}
add_filter( 'woocommerce_gateway_title', 'add_ppcp_gateway_title_link', 10, 2 );
@InpsydeNiklas
InpsydeNiklas / functions.php
Created February 1, 2023 10:28
display icon for ppcp-card-button-gateway in the checkout
<?php
function woocommerce_paypal_payments_card_button_gateway_icon( $icon, $id ) {
if ( $id === 'ppcp-card-button-gateway' ) {
return '<img src="https://upload.wikimedia.org/wikipedia/commons/2/2a/Mastercard-logo.svg" alt="Mastercard" /> <img src="https://upload.wikimedia.org/wikipedia/commons/0/04/Visa.svg" alt="Visa" /> <img src="https://www.paypalobjects.com/webstatic/mktg/Logo/pp-logo-100px.png" alt="PayPal Payments" />';
} else {
return $icon;
}
}
add_filter( 'woocommerce_gateway_icon', 'woocommerce_paypal_payments_card_button_gateway_icon', 10, 2 );
@InpsydeNiklas
InpsydeNiklas / functions.php
Last active January 27, 2023 16:32
disable PayPal Payments (incl. PayPal Card Processing) for product with a given ID (mini cart button may still render)
<?php
// removes PayPal smart buttons on single product page for a product with a given id
function ppcp_remove_single_product_buttons() {
$product = wc_get_product();
if ($product) {
if ( $product->get_id() == 24 ) {
return false;
}
else { return true;}