Actions & filters
Last modified:
Action & filter hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively throughout WordPress and WooCommerce and are very useful for developers.
The Swedbank Pay Payment Menu plugin includes action and filter hooks that let you, as a merchant or developer, modify the data sent in requests between your webshop and Swedbank Pay’s system.
To add a filter you can use the Code Snippets plugin, or something similar of your choice.
If you are new to filters in WordPress you can read more in this article.
Read more about action and filter hooks here:
How and where to insert the code?
To get the functions exemplified in this section to work you need to add the code to your theme’s functions.php. You can add it as its own plugin or through the Code Snippets plugin, or something similar of your choice.
Actions
Set or override the subsite for payment orders
swedbank_pay_payee
When a payment order is created, the plugin sends payee information to Swedbank Pay, including the Subsite entered in the plugin settings, if any. The swedbank_pay_payee filter lets you set the subsite programmatically instead of using the setting — or override it dynamically, for example based on the order.
The filter receives the payee information object after the setting has been applied, so any subsite you set in the filter takes precedence. It runs both when a checkout session is created and when a payment order is created from an existing order.
/**
* Set the subsite for Swedbank Pay payment orders.
*
* Note: the value must contain only letters and numbers, with a maximum
* of 40 characters. The settings field validates this, but the filter
* does not — an invalid value will be rejected by the Swedbank Pay API.
*
* @param KrokedilSwedbankPayDeps\SwedbankPay\Api\Service\Paymentorder\Resource\PaymentorderPayeeInfo $payee The payee information object.
* @param Krokedil\Swedbank\Pay\Helpers\Order|Krokedil\Swedbank\Pay\Helpers\Cart $helper The payment data helper the payee info was built from.
*/
add_filter(
'swedbank_pay_payee',
function ( $payee, $helper ) {
$payee->setSubsite( 'MySubsite' );
return $payee;
},
10,
2
);The second parameter lets you make the subsite conditional. For order-based payments, it’s an Order helper that gives you access to the WooCommerce order:
add_filter(
'swedbank_pay_payee',
function ( $payee, $helper ) {
if ( $helper instanceof Krokedil\Swedbank\Pay\Helpers\Order ) {
$order = $helper->get_order();
if ( 'SEK' === $order->get_currency() ) {
$payee->setSubsite( 'MySwedishSubsite' );
}
}
return $payee;
},
10,
2
);