Actions & filters

Last modified:

Action and 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 similar of your choice.

Actions

Display missing Gift Cards form

display_gift_card_form

When using Qliro as the payment method, the WooCommerce Gift Cards plugin form (“Have a gift card?”) is not displayed by default. As a result, customers cannot enter a gift card code from this plugin during checkout. This should not be confused with the default WooCommerce coupon field, which may still be visible.

The display_gift_card_form example below shows how to display the WooCommerce Gift Cards form on the Qliro checkout page. In the example, the form is rendered after the order review, but you can change its placement by hooking into a different Qliro action.

Available placements

You can change where the gift card form is displayed by modifying the first parameter in the add_action call. The following action hooks are available:

  • qliro_one_wc_before_wrapper
  • qliro_one_wc_before_order_review
  • qliro_one_wc_after_order_review
  • qliro_one_wc_before_snippet
  • qliro_one_wc_after_snippet
  • qliro_one_wc_after_wrapper
  • qliro_one_wc_after_checkout_form

Example

<?php
/**
 * Display WooCommerce Gift Cards form.
 *
 * @return void
 */
function display_gift_card_form() {
	if ( function_exists( 'WC_GC' ) ) {
		WC_GC()->cart->display_form();
	}
}

add_action( 'qliro_one_wc_after_order_review', 'display_gift_card_form' );

Filters

Action filters are used to modify data before sending it to an external service, rendering it in the browser or storing it in a database.

Automatically sync orders on update

qliro_sync_order_on_update

If you want to allow automatic syncing of WooCommerce orders to Qliro whenever an order is updated you can use the qliro_sync_order_on_update filter.

// Enable the automatic order sync for all Qliro orders.
add_filter( 'qliro_sync_order_on_update', '__return_true' );

This filter eliminates the need to manually sync orders from WooCommerce to Qliro.

You can also limit the automatic sync to orders based on custom criteria. In the example the order is synced based on a custom meta field:

/**
 * Enable automatic order sync for specific orders based on custom criteria.
 * 
 * @param bool $should_sync If the order should be synced automatically or not. Default is false.
 * @param int  $order_id    The WooCommerce order id to maybe sync automatically.
 */
function qliro_enable_automatic_order_sync($should_sync, $order_id)
{
	$order = wc_get_order( $order_id );
	
	// Example condition: Sync if the order has a specific custom meta field value.
	if ( $order && 'custom_meta_value' === $order->get_meta( 'custom_meta_field' ) ) {
		return true;
	}

	return $should_sync;
}
add_filter( 'qliro_sync_order_on_update', 'qliro_enable_automatic_order_sync', 10, 2 );

Display Qliro even on free orders

qliro_check_if_needs_payment

If you have a coupon that brings the total order value down to zero making the order free for the customer, by default this will show the standard WooCommerce checkout instead of Qliro. If you would rather want to show Qliro for this you can use the filter qliro_check_if_needs_payment. And return false instead of true to this filter.

/**
 * Filter to change if you want the Qliro checkout to display on free orders or not.
 * For example after a coupon has been used.
 */

add_filter( 'qliro_check_if_needs_payment', 'qliro_change_check_if_needs_payment' );
function qliro_change_check_if_needs_payment( $bool ) {
  return false;
}

Please note that if you use this filter, Qliro is required to be the first active payment method in WooCommerce under WooCommerce → Settings → Payments. Otherwise this filter won’t work.

Modify data sent to Qliro

Filter to force the Qliro country

qliro_one_request_args

If you want to overwrite the WooCommerce default and instead force a Qliro country you can use the qliro_one_request_args filter.

/** 
 * Filter to force the Qliro country. Overwrites the WooCommerce default, and sets a forced country.
 * Change NO to whatever value you want, 2 letter country codes only. Make sure Qliro is available in that country.
 */

add_filter( 'qliro_one_request_args', 'qliro_one_forced_country_request_args' );

/**
 * Forces the Country.
 *
 * @param array $request_args
 * @return array
 */

function qliro_one_forced_country_request_args( $request_args ) {
    $request_args['Country'] = 'NO';
    return $request_args;
}

Filter to force the Qliro language

qliro_one_request_args

If you want to overwrite the WooCommerce default and instead force a Qliro language you can use the qliro_one_request_args filter.

/** 
 * Filter to force the Qliro language. Overwrites the WooCommerce default, and sets a forced language.
 * Change 'en-us' to any of the Qliros supported languages.
 * https://developers.qliro.com/docs/qliro-checkout/get-started/checkout-basics#supported-languages
 */

add_filter( 'qliro_one_request_args', 'qliro_one_forced_language_request_args' );

/**
 * Forces the language.
 *
 * @param array $request_args
 * @return array
 */

function qliro_one_forced_language_request_args( $request_args ) {
    $request_args['Language'] = 'en-us';
    return $request_args;
}

Filter to modify the order items payload

There are two filters available for modifying the order items payload that is sent to the Qliro API. Both filters only affect the data sent to Qliro and do not modify the WooCommerce cart, order data, or any stored values.

Use the qliro_one_helper_cart_items filter before an order exists, and the qliro_one_helper_order_items filter once an order has been created.

qliro_one_helper_cart_items

This filter allows you to modify the order items payload during checkout, before a WooCommerce order has been created. It works on the cart data and is applied when the order lines are prepared for the Qliro API request.

Use this filter to add, remove, or adjust line items (for example fees or discounts), or to anonymize data before it is sent to Qliro. This filter is only applied during checkout and does not apply to “pay for order” purchases.

<?php
/**
 * Modify cart items example.
 *
 * This function modifies the cart items array.
 *
 * @param array $items Array of line items in the format:
 *                     array (
 *                       'MerchantReference' => 'simple-25',
 *                       'Description'       => 'Simple 25%',
 *                       'Type'              => 'Product',
 *                       'Quantity'          => 1,
 *                       'PricePerItemIncVat'=> '124.99',
 *                       'PricePerItemExVat' => '99.99',
 *                       'VatRate'           => '25.00',
 *                     ).
 * @return array Modified array of line items.
 */
function qliro_modify_cart_items_example( $items ) {
	return $items;
}

add_filter( 'qliro_one_helper_cart_items', 'qliro_modify_cart_items_example' );

qliro_one_helper_order_items

This filter allows you to modify the order items payload based on an existing WooCommerce order. It is applied after purchase during order management, and is also used for “pay for order” purchases and subscription renewals.

Use this filter to add, remove, or adjust order lines (for example fees or discounts), or to anonymize data before the data is sent to Qliro.

<?php
/**
 * Modify order items example.
 *
 * This function modifies the order items array.
 *
 * @param array $items Array of order items in the format:
 *                     array (
 *                       'MerchantReference' => 'order-123',
 *                       'Description'       => 'Order Description',
 *                       'Type'              => 'Product',
 *                       'Quantity'          => 2,
 *                       'PricePerItemIncVat'=> '249.99',
 *                       'PricePerItemExVat' => '199.99',
 *                       'VatRate'           => '25.00',
 *                     ).
 * @return array Modified array of order items.
 */
function qliro_modify_order_items_example( $items ) {
	return $items;
}

add_filter( 'qliro_one_helper_order_items', 'qliro_modify_order_items_example' );