Hooks (action & filter)

Hooks (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.

There are several action filters available in the Nets Easy plugin. These filters are mainly added so that you as a merchant or developer have the possibility to change what is sent in the requests between your webshop and Nets 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.


Filter


Modify order / cart data sent in initial create payment request

<?php
/**
 * Filter hook dibs_easy_create_order_args
 * Modify the order/cart data sent to Nets.
 *
 * Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
 */
add_filter( 'dibs_easy_create_order_args', 'my_dibs_easy_create_order_args' );

function my_dibs_easy_create_order_args( $request_args ) {

    // Default value = false, if set to true the checkout will not load any user data.
    $request_args['checkout']['publicDevice'] = true;

    return $request_args;
}

Example how to charge the transaction automatically after reservation have been accepted without calling the Charge API. This will not work if the store offer invoice payments via Nets Easy.

<?php
/**
 * Filter hook dibs_easy_create_order_args
 * Modify the order/cart data sent to Nets.
 *
 * Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
 */
add_filter( 'dibs_easy_create_order_args', 'my_dibs_easy_create_order_args_2' );

function my_dibs_easy_create_order_args_2( $request_args ) {

    // Default value = false, if set to true the transaction will be charged automatically after reservation have been accepted without calling the Charge API.
	// Does not work if payment method is invoice.
    $request_args['checkout']['charge'] = true;

    return $request_args;
}

Example on how to set a 00000 placeholder for countries that lack postal code, e.g. Qatar or UAE.

<?php
/**
 * Filter hook dibs_easy_create_order_args
 * Modify the order/cart data sent to Nets.
 *
 * Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
 */
add_filter( 'dibs_easy_create_order_args', 'my_dibs_easy_create_order_args' );

function my_dibs_easy_create_order_args( $request_args ) {

    // Default value = false, if set to true the checkout will not load any user data.
    if ($request_args['checkout']['consumer']['shippingAddress']['country'] == 'ARE' {
    	$request_args['checkout']['consumer']['shippingAddress']['postalCode'] = '00000';
    }

    return $request_args;
}

Modify order / cart data in update payment request

<?php
/**
 * Filter hook dibs_easy_update_order_args
 * Modify the order/cart data sent to Net on update requests.
 *
 * Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
 */
add_filter( 'dibs_easy_update_order_args', 'my_dibs_easy_update_order_args' );

function my_dibs_easy_update_order_args( $request_args ) {

    // Confirm that shipping cost is specified.
    $request_args['shipping']['costSpecified'] = true;

    return $request_args;
}

Modify the timeout time for all http requests sent to Nets

  • nets_easy_set_timeout
  • Usage: Modify the timeout time for all http requests sent to Nets (measured in seconds). Used together with the Nets Easy for WooCommerce plugin.
<?php
/**
 * Filter hook nets_easy_set_timeout
 * Modify the timeout time used during the order creation process in Woocommerce.
 *
 * Used together with Nets Easy for WooCommerce plugin.
 *
 * Add this code to your themes functions.php file or include it in a separate functionality plugin (https://css-tricks.com/wordpress-functionality-plugins/).
 */
add_filter( 'nets_easy_set_timeout', 'custom_nets_easy_set_timeout' );
function custom_nets_easy_set_timeout( $time ) {
    return 20;
}
?>

Make the checkout address field “State” not required

When you are selling to some countries the checkout field State in the address is required in WooCommerce. Nets does not have the field State in their system, only City and Country.

With this filter you can make the State field optional to avoid a checkout error in the communication with Nets when you have the Embedded checkout flow.

add_filter( 'woocommerce_billing_fields', 'nets_filter_state_billing', 10, 1 );
 
add_filter( 'woocommerce_shipping_fields', 'nets_filter_state_shipping', 10, 1 );
 
 
 
function nets_filter_state_billing( $address_fields ) {
 
                $address_fields['billing_state']['required'] = false;
 
                return $address_fields;
 
}
 
 
 
function nets_filter_state_shipping( $address_fields ) {
 
                $address_fields['shipping_state']['required'] = false;
 
                return $address_fields;
 
}