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.

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 similiar of your choice.

Filters

Modify order & cart data sent to Avarda

aco_create_args

Used to modify the order data that is sent to Avarda in the initialize payment request. In this example we add a newsletter signup checkbox.

/**
 * Add a newsletter signup checkbox to Avardas checkout.
 * If the checkbox was checked or not by the customer can be retrieved via the aco_wc_confirm_avarda_order hook.
 *
 * @param array $request_body The payload that will be sent to Avarda.
 * @param int   $order_id The WooCommerce order ID.
 *
 * @return array
 */
add_filter( 'aco_create_args', 'custom_aco_create_args', 10, 2 );

function custom_aco_create_args( $request_body, $order_id = 0 ) {
    if ( $order_id ) {
        // If we have a valid order, we can retrieve the order object.
    } else {
        // If no order ID is provided, we should be able to retrieve data from the cart object.
    }
    // Here we can modify the request body as needed.
    $request_body['checkoutSetup']['EmailNewsletterSubscription']     = 'unchecked';
    $request_body['checkoutSetup']['emailNewsletterSubscriptionText'] = __( 'I want to receive the newsletter', 'kroconnect' );
    return $request_body;
}

Allow free orders in Avarda Checkout

aco_allow_free_orders

As a WooCommerce default, when an order has 0 value (free trial, applied coupons…), Avarda Checkout is not loaded. Instead the standard WooCommerce checkout is loaded. If you have Avarda as the only payment method this means that the customer can’t complete the order.

With the filter below you allow free orders with Avarda Checkout.

/**
 * Allow free orders using Avarda Checkout for WooCommerce.
 *
 * @return bool
 */
function aco_allow_free_orders() {
	return true;
}

add_filter( 'woocommerce_order_needs_payment', 'aco_allow_free_orders', 999 );
add_filter( 'woocommerce_cart_needs_payment', 'aco_allow_free_orders', 999 );

Change Avarda credentials

aco_credentials

The aco_credentials filter allows you to modify the Avarda API credentials used by the plugin, based on specific conditions such as country, language, or test mode.

In the example below, the filter checks whether the current URL contains /sv/.
If it does not, the function replaces the default Avarda credentials with an alternative set – for example, credentials used for an English or international version of the site.

This can be useful if you have different Avarda accounts or API credentials for different store languages or markets, and you want the plugin to automatically select the correct credentials at runtime.

/**
 * Change credentials based on the country that was matched by the currency by the plugin.
 *
 * @param array  $credentials The credentials. Will be an array with the keys client_id and client_secret.
 * @param string $country The country based on the currency and what is supported by Avarda. Can only be se, no, dk or fi. Default is se.
 * @param bool   $international If the request is international or not. Based on if the country has any credentials entered for it.
 * @param string $testmode If the request is in testmode or not. yes or no.
 *
 * @return array
 */
function aco_change_credentials_uri( $credentials, $country, $international, $testmode ) {
	// Check if current URI contains /sv/ and if false, switch Avarda credentials
	if( strpos($_SERVER['REQUEST_URI'], '/sv/') === false ) {
		// Change the credentials to the English ones.
		$credentials['client_id'] = 'xxxx';
		$credentials['client_secret'] = 'xxxx';
	}
	return $credentials;
}
add_filter( 'aco_credentials', 'aco_change_credentials_uri', 10, 4 );

Set custom payment method title

aco_order_set_payment_method_title

If you want to set the payment method title based on the payment method or order data you can use the aco_order_set_payment_method_title filter.

This can be used to set custom titles for any payment method, or apply custom logic based on the order.

/**
 * Filter the payment method title based on the payment method or order data.
 * This can be used to set custom titles for any payment method or apply custom logic based on the order.
 * 
 * This will override the default payment method title set by WooCommerce or the payment gateway.
 * 
 * @param string $method_title_filtered The filtered payment method title.
 * @param string $method_title The original payment method title.
 * @param int    $order_id The WooCommerce order ID.
 * 
 * @return string The custom payment method title.
 */
add_filter(
    'aco_order_set_payment_method_title',
    function ( $method_title_filtered, $method_title, $order_id ) {
        if ( 'Swish' === $method_title ) {
            return 'A custom payment method title for Swish';
        }

        return $method_title_filtered;
    },
    10,
    3
);

Set shipping method carrier

custom_aco_shipping_method_carrier

If you are displaying shipping in the embedded Avarda Checkout and want to filter the shipping method carrier to be set for the shipping rate in the Avarda shipping module, you can use the filter below.

By changing the $carrier value (in this example “my_carrier_id”) you can set a shipping method carrier of your choosing.

/**
 * Filter the shipping method carrier to be set for the shipping rate in the Avarda shipping module. This can be used to set the carrier for a shipping rate to use the included carrier icons.
 * Refer to the ACO_Shipping_Option_Model::get_shipping_method_icon method here to see the available default carriers: https://github.com/krokedil/avarda-checkout-for-woocommerce/blob/master/classes/api/models/shipping/class-aco-shipping-option-model.php
 * You can also filter the icon if needed if you want to set your own carrier id and icon.
 * 
 * If no carrier is set from the filter the plugin will look at the rate and try to get the carrier from the metadata keys 'carrier' or 'udc_carrier_id'. 
 * Which can also be used instead of the filter when the shipping rate is calculated if needed.
 * 
 * @param string $carrier The carrier to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the carrier for from WooCommerce
 * 
 * @return string The carrier to be set for the shipping rate.
 */
function custom_aco_shipping_method_carrier( $carrier, $shipping_rate ) {
	if( $shipping_rate->get_method_id() === 'my_shipping_id' ) {
		$carrier = 'my_carrier_id';
	}
	
	return $carrier;
}
add_filter( 'aco_shipping_method_carrier', 'custom_aco_shipping_method_carrier', 10, 2 );

Set description for shipping method

custom_aco_shipping_method_description

If you are displaying shipping in the embedded Avarda Checkout and want to set your own shipping method description you can use the filter below.

By changing the $description value (in this example “My filtered description”) you can set a description of your choosing.

/**
 * Add a custom description to your shipping method in the Avarda Checkout.
 * This will be displayed in the shipping method when its listed by the checkout.
 * 
 * This will override the setting that the plugin adds to shipping rates.
 * 
 * @param string $description The description to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the description for from WooCommerce
 * 
 * @return string The description to be set for the shipping rate.
 */
function custom_aco_shipping_method_description( $description, $shipping_rate ) {
	// If the shipping method id is my_shipping_id, set the description to a custom description.
	if( $shipping_rate->get_method_id() === 'my_shipping_id' ) {
		$description = 'My custom description';
	}
	
	return $description;
}
add_filter( 'aco_shipping_method_description', 'custom_aco_shipping_method_description', 10, 2 );

Set icon for shipping method

custom_aco_shipping_method_icon

If you are displaying shipping in the embedded Avarda Checkout and want to set an icon for the shipping method you can use the filter below.

By changing the $img_url value (in this example “https://example.com/my-icon.png”) you can set an icon of your choosing.

/**
 * Filter the shipping method icon url based on the carrier id or some data from the shipping rate.
 * This can be used to set your own icons for any carrier, even the once we add default icons for. Or for any custom logic on the shipping rate itself if needed.
 * 
 * This will override the setting that the plugin adds to shipping rates.
 * 
 * @param string $img_url The icon url to be set for the shipping rate.
 * @param string $carrier The carrier to be set for the shipping rate.
 * @param WC_Shipping_Rate $shipping_rate The shipping rate to set the icon for from WooCommerce
 * 
 * @return string The icon url to be set for the shipping rate.
 */
function custom_aco_shipping_method_icon( $img_url, $carrier, $shipping_rate ) {
	// If the carrier is set to my_carrier_id or the shipping method id is my_shipping_id, set the icon to a custom icon.
	if( $shipping_rate->get_method_id() === 'my_shipping_id' || $carrier === 'my_carrier_id' ) {
		$img_url = 'https://example.com/my-icon.png';
	}
	
	return $img_url;
}
add_filter( 'aco_shipping_icon', 'custom_aco_shipping_method_icon', 10, 3 );

Actions

Save custom data from Avarda order to WooCommerce order

custom_aco_wc_confirm_avarda_order

Custom function to be able to save additional data to the order when the Avarda order is confirmed.

add_action( 'aco_wc_confirm_avarda_order', 'custom_aco_wc_confirm_avarda_order', 10, 2 );

/**
 * Custom function to be able to save additional data to the order when the Avarda order is confirmed.
 *
 * @param int   $order_id The ID of the WooCommerce order.
 * @param array $avarda_order The Avarda order data.
 */
function custom_aco_wc_confirm_avarda_order( $order_id, $avarda_order ) {
    // This function is called when the Avarda order is confirmed.
    // We can use this to modify the order or perform additional actions.
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }

    // Save the Avarda customer mode (B2B or B2C) to the order meta.
    $mode = $avarda_order['mode'] ?? 'B2C'; // Default to B2C if not set.
    $order->update_meta_data( 'avarda_customer_mode', $mode );

    // Save the email newsletter subscription status to the order meta.
    // This info is only available if you have added it via the aco_create_args filter.
    $email_newsletter_status = $avarda_order[$mode]['step']['emailNewsletterSubscription'] ?? 'Unchecked';
    $order->update_meta_data( 'avarda_email_newsletter_subscription', $email_newsletter_status );

    // Save the entire Avarda order to the order meta (as json).
    $order->update_meta_data( 'avarda_order', wp_json_encode( $avarda_order) );
    $order->save_meta_data();
}