Skip to content

Actions and Filters

Narek Zakarian edited this page Oct 7, 2022 · 46 revisions

Adding modules and accessing the plugin container

To access the container from the outside, add your module using a filter provided below.

Inside your module, you will be able to access and extend or modify the container. This is a simplified example:

//composer.json

{
  "name": "vendor_name/test-plugin",
  "description": "description",
  "minimum-stability": "stable",
  "require": {
    "dhii/module-interface": "0.3.x-dev"
  }
}

//your-plugin.php

$module = new class implements Dhii\Modular\Module\ModuleInterface{
	public function setup(): ServiceProviderInterface
	{
		return new ServiceProvider(
			[], //place here your services you want to add to container,
                        [] // place here your extensions to replace existing container services (see links below for more details).
		);
	}
	
	public function run(ContainerInterface $c): void
	{
                //here you can get the container instance and do something with it.
		add_action('admin_notices', function() use ($c){
			$c->get('admin-notices.renderer')->render();
		});
	}
};

add_filter('woocommerce_paypal_payments_modules', function($modules) use ($module) {
	array_push($modules, $module);
	
	return $modules;
});

See https://github.com/container-interop/service-provider and https://github.com/Dhii/module-interface for more details.

Change button placement via render hooks

The default smart button placement can be modified with these available render hooks:

  • woocommerce_paypal_payments_checkout_button_renderer_hook
  • woocommerce_paypal_payments_checkout_dcc_renderer_hook
  • woocommerce_paypal_payments_pay_order_dcc_renderer_hook
  • woocommerce_paypal_payments_proceed_to_checkout_button_renderer_hook
  • woocommerce_paypal_payments_mini_cart_button_renderer_hook
  • woocommerce_paypal_payments_single_product_renderer_hook

Example filter to display the smart buttons on the single product page below the add to cart button:

add_filter('woocommerce_paypal_payments_single_product_renderer_hook', function() {
    return 'woocommerce_after_add_to_cart_button';
});

Depending on the theme, this may need custom CSS to add a margin to the #ppc-button-ppcp-gateway div.

This page lists all available WooCommerce hooks. Some themes or page builders may not provide all default hooks, which can result in the buttons not rendering.

Add advanced credit and debit card (ACDC) countries

PayPal Payments enables the PayPal Card Processing for the officially supported countries/currencies as listed here: https://developer.paypal.com/docs/checkout/advanced/#link-eligibility

The filters below extend the supported country and currency list from within the plugin. Whether or not the PayPal account is operating from an supported or unsupported region, PayPal must first approve the application for advanced card payments.

woocommerce_paypal_payments_supported_country_currency_matrix - To filter supported countries and their currencies.

woocommerce_paypal_payments_supported_country_card_matrix - To filter supported countries and their cards.

Here is the example of how the filters can be used to allow ACDC for Singapore.

add_filter('woocommerce_paypal_payments_supported_country_currency_matrix', static function(array $countries): array {

    $countries['SG'] = [
        'AUD',
        'CAD',
        'CHF',
        'CZK',
        'DKK',
        'EUR',
        'GBP',
        'HKD',
        'HUF',
        'JPY',
        'NOK',
        'NZD',
        'PLN',
        'SEK',
        'SGD',
        'USD',
    ];

    return $countries;
});

add_filter('woocommerce_paypal_payments_supported_country_card_matrix', static function(array $countries): array {

    $countries['SG'] = [
        'mastercard' => [],
        'visa'       => [],
        'amex'       => array( 'AUD' ),
    ];

    return $countries;
});

Modify the order creation request body data

apply_filters( 'ppcp_create_order_request_body_data', array $data ) Filters the order creation request body data.

Parameters

$data. (array) The order creation request body data.

More Information

The filter will allow to modify the request body data before it is sent to PayPal. The structured data is created in regards of how PayPal API expects it so if after modification it contains the incorrect parts then the error message will be shown.

Example

Filter is used to modify the Brand Name to be New Brand Name

add_filter('ppcp_create_order_request_body_data', static function (array $data): array {
    $data['application_context']['brand_name'] = 'New Brand Name';
    return $data;
});

Result:

Screen Shot 2022-03-16 at 15 58 19

Disable basic Checkout form field validation before creating the PayPal order

PayPal Payments version 1.9.0 added a basic form field validation to improve the behavior outlined in #513.

This basic validation will prevent the user from attempting the payment by throwing an error similar to WooCommerce when not all fields are filled (e.g. terms checkbox).

The feature is enabled by default and can be disabled with the filter below:

add_filter( 'woocommerce_paypal_payments_basic_checkout_validation_enabled', '__return_false' );

Disable smart buttons on the single product page for specific products

The woocommerce_paypal_payments_product_supports_payment_request_button hook can be used to hide the smart buttons on specific product pages. In the example below, the buttons are hidden for subscription-type products:

function ppcp_remove_single_product_buttons( $enable_button, $product ){
    if ( $product->is_type( 'subscription' ) ) {
       $enable_button = false;
    }
    return $enable_button;
}
add_action( 'woocommerce_paypal_payments_product_supports_payment_request_button', 'ppcp_remove_single_product_buttons', 10, 2 );

The Cart, Mini Cart, and Checkout buttons are not affected by this filter.

Adding a gateway icon

While PayPal Payments does not provide a default payment gateway icon, you can add one with a filter like the one below. Exchange the img src for any icon URL you prefer.

function woocommerce_paypal_payments_gateway_icon( $icon, $id ) {
    if ( $id === 'ppcp-gateway' ) {
        return '<img src="' . esc_url( plugins_url( 'woocommerce-paypal-payments/modules/ppcp-wc-gateway/assets/images/paypal.png', ) ) . '" > ';
	}
		if ( $id === 'ppcp-pay-upon-invoice-gateway' ) {
       return '<img src="' . esc_url( plugins_url( 'woocommerce-paypal-payments/modules/ppcp-wc-gateway/assets/images/paypal.png', ) ) . '" > ';
    } else {
        return $icon;
    }
}
add_filter( 'woocommerce_gateway_icon', 'woocommerce_paypal_payments_gateway_icon', 10, 2 );

The ppcp-pay-upon-invoice-gateway is only relevant for merchants from Germany.

The PayPal Card Processing feature already provides a selection of default credit card icons depending on the eligibility.

Add custom Tracking Carrier/Status

PayPal Payments offers a Tracking integration to send shipment data to PayPal. These filters can add a new carrier and a new shipping status:

add_filter('ppcp_tracking_carriers', static function(array $carriers): array {
    $carriers['DE']['items']['custom'] = 'Custom';
    return $carriers;
});

add_filter('ppcp_tracking_statuses', static function(array $statuses): array {
    $statuses['custom'] = 'Custom';
    return $statuses;
});

Modify tracking data before creating/updating in PayPal

Parameters

$data. (array) The tracking data. (transaction_id, status, tracking_number, carrier)

Please note: Status can only be changed to the values PayPal allows

Please note: Carrier can only be changed to the values PayPal allows

More Information

The filter will allow to modify the tracking data before it is sent to PayPal. The structured data is created in regards of how PayPal API expects it so if after modification it contains the incorrect parts then the error message will be shown.

Example

Filter is used to modify the Status to be DELIVERED when adding for first time.

add_filter('woocommerce_paypal_payments_tracking_data_before_sending', static function(array $data, int $order_id){
    $data['status'] = "DELIVERED";
    return $data;
});

Filter is used to modify the Status to be DELIVERED when updating the existing tracking.

add_filter('woocommerce_paypal_payments_tracking_data_before_update', static function(array $data, int $order_id){
    $data['status'] = "DELIVERED";
    return $data;
});
Clone this wiki locally