Actions & filters
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 Walley
walley_initialize_checkout_args
Used to modify the order data that is sent to Walley in the initialize checkout session request. In this example we add a settlementReference to the request. Available parameters can be found in Walley’s API docs.
/**
* Add settlementReference in initialize request to Walley.
*
* @param array $request_body The payload that will be sent to Walley.
* @param int $order_id The WooCommerce order ID.
*
* @return array
*/
add_filter( 'walley_initialize_checkout_args', 'custom_walley_initialize_checkout_args', 10, 2 );
function custom_walley_initialize_checkout_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['settlementReference'] = 'shop1';
return $request_body;
}
walley_update_checkout_args
Used to modify the order data that is sent to Walley in the update checkout session request. In this example we add a settlementReference to the request. Available parameters can be found in Walley’s API docs.
/**
* Add metadata in update request to Walley.
*
* @param array $request_body The payload that will be sent to Walley.
* @param int $order_id The WooCommerce order ID.
*
* @return array
*/
add_filter( 'walley_update_checkout_args', 'custom_walley_update_checkout_args', 10, 2 );
function custom_walley_update_checkout_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['metadata']['my_custom_data_name'] = 'my_custom_data_value';
return $request_body;
}
Actions
Save custom data from Walley order to WooCommerce order
walley_after_confirm
Custom function to be able to save additional data to the order when the Walley order is confirmed. Information about available parameters can be found here.
add_action( 'walley_after_confirm', 'custom_walley_after_confirm', 10, 2 );
/**
* Custom function to be able to save additional data to the order when the Walley order is confirmed.
*
* @param object $order The WooCommerce order.
* @param array $walley_order The Walley order data.
*/
function custom_walley_after_confirm( $order, $walley_order ) {
$customer_type = $walley_order['data']['customerType'] ?? '';
if( 'PrivateCustomer' !== $customer_type ) {
// If the customer type is not 'PrivateCustomer', do not save the data.
return;
}
$national_identification_number = $walley_order['data']['customer']['nationalIdentificationNumber'] ?? '';
// Save the national identification number to the order meta.
$order->update_meta_data( 'walley_national_identification_number', $national_identification_number );
$order->save_meta_data();
}