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.

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.


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. The most common occasion this might be used in the plugin is if you want to modify the order/cart data sent to Payson before the checkout is rendered.


Age verification – validate age with Personal identity number

Code example on how to only accept purchases from people over 18 years of age with Payson Checkout for WooCommerce

/**
 * Validates the age of the customer. Prevents customers from under the given age to complete an order.
 *
 * @return void
 */
function pco_validate_age() {
	$payson_payment_id = WC()->session->get( 'payson_payment_id' );
	$payson_order      = pco_wc_get_order( $payson_payment_id );
	if ( is_wp_error( $payson_order ) ) {
		return;
	}

	$identity_number = $payson_order['customer']['identityNumber'] ?? false;
	if ( empty( $identity_number ) ) {
		return;
	}

	// Extract the birthdate. Expected format YYMMDDNNNN or YYYYMMDDNNNN.
	$today = new DateTime();
	if ( strlen( $identity_number ) <= 10 ) {
		$year = intval( substr( $identity_number, 0, 2 ) );

		// Assuming the customer is not older than 100 years.
		if ( $year > intval( gmdate( 'y' ) ) ) {
			$birthdate = new DateTime( '19' . substr( $identity_number, 0, 6 ) );
		} else {
			$birthdate = new DateTime( '20' . substr( $identity_number, 0, 6 ) );
		}
	} else {
		$birthdate = new DateTime( substr( $identity_number, 0, 8 ) );
	}

	$minimum_age = 18;
	$age         = $today->diff( $birthdate );
	if ( $age->y < $minimum_age ) {
		wc_add_notice( __( 'You must be 18 years or older.', 'woocommerce-gateway-paysoncheckout' ), 'error' );
	}

}

add_action( 'woocommerce_checkout_process', 'pco_validate_age' );

Change the checkout expirationTime

The default checkout expirationTime is set to 3 hours. To change this you can add this filter and set your own expirationTime. In the example below it is set to 24 hours.

add_filter( 'pco_create_order_args', 'pco_set_expiration_time' );
/**
 * Function to set the expiration time for a PCO order.
 * 
 * @param array $args The request args for creating a Payson order. The body is a JSON string under the key 'body'.
 * @return array
 */
function pco_set_expiration_time( $args ) {
    // Decode the body.
    $body = json_decode( $args['body'], true );
    
    // Set this to the number of hours in the future that you want the expiration time to be set for.
	$expiration_time_hours   = 24;
    
    // Create a ISO 8601 date string.
    $body['expirationTime'] = date( 'c', time() + HOUR_IN_SECONDS * $expiration_time_hours );
    
    // Encode the body and set it as the body for the request.
    $args['body'] = wp_json_encode( $body );
    
    return $args;
}

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. Payson 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 Payson when you have the Embedded checkout flow.

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