Add custom parameters on WC Messaging template’s

The WC Messaging plugin integrates WhatsApp Cloud Business API with WooCommerce. It sends free automated WhatsApp notifications for WooCommerce orders and provides custom trigger buttons for easily sending custom pre-defined message templates to customers.

Custom templates’ messages are able to be sent via WC Messaging. The unique features of WC Messaging are template messages with customisable parameters. To create a custom parameter, you need to include a filter hook in your code. The code demonstrates how to include customised parameters when triggering template messages, as follows:

add_filter('woom_additional_template_params', 'function_name', 10, 2);
function function_name($parameters, $order)
{

    $custom_params = array(
        "param1" => '',
        "param2" => ''
    );
    if ($order !== null) {
        $custom_params = array(
            "param1" => 'value1',
            "param2" => 'value2'
        );
    }
    $parameters = array_merge($parameters, $custom_params);
    return $parameters;
}

The code mentioned above demonstrates how to provide customised parameters when selecting a template message on the WC Messaging template setting page. Once you’ve specified the parameters you require, you can select the template messages and matching parameters as needed.

Let’s go through an example:

add_filter('woom_additional_template_params', 'function_name', 10, 2);
function function_name($parameters, $order){
    $custom_params = array(
        "booking_id" => '',
        "booking_name" => ''
    );
    if ($order !== null) {
      $custom_params = array(
          "booking_id" => 123,
          "booking_name" => 'developer'
      );
   }
$parameters = array_merge($parameters, $custom_params);
return $parameters;
}

After adding the filter hook ‘woom_additional_template_params’ to my functions and calling the function ‘function_name’, I received the output shown below.

The corresponding parameters appeared to be generated when I added booking_id and booking_name. 

Let’s consider one advanced example:

Here is an example of how to retrieve details about a WooCommerce booking, such as the booking ID, booking address, booking date, etc., for a particular order. Let’s take a look at how to do this in a standardized manner.

add_filter('woom_additional_template_params', 'woom_booking_params', 10, 2);
 function woom_booking_params($params, $order)
 {
     $custom_params = array();
     if ($order !== null && is_callable('WC_Booking_Data_Store::get_booking_ids_from_order_id')) {
         $booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id($order->get_id());
         $booking = new WC_Booking($booking_ids[0]);
         $custom_params = array(
             "booking_id" => $booking_ids[0],
             "booking_date" => gmdate("d-M-Y", $booking->data['start']),
             "booking_address" => get_post_meta($booking->get_product_id()),
             "location" => "https://maps.google.com/?q=",
 
         );
         $params = array_merge($params, $custom_params);
     } else {
         $custom_params = array(
             "location" => '',
             "booking_address" => '',
             "booking_date" => '',
             "booking_id" => '',
         );
         $params = array_merge($params, $custom_params);
     }
 
     return $params;
 }

I added this code,then i got the below params to my template page

Here is an example of how to find the order total in WooCommerce. The order total is assigned to the variable order_total. When these parameters are passed, the total of an order is retrieved. Let’s go through that example in detail.

add_filter('woom_additional_template_params', 'woom_add_order_total_to_params', 10, 2);
function woom_add_order_total_to_params($parameters, $order)
{
    $custom_params = array(
        "order_total" => '',
    );
    if ($order !== null) {
        $custom_params = array(
            "order_total" => $order->get_total()

        );
    }
    $parameters = array_merge($parameters, $custom_params);
    return $parameters;
}

This code snippet adds the order total to the existing parameters array used in a WooCommerce context, making it available for templates or other uses where this filter is applied.

Now, the order total is available in a variable named order_total.

All the examples are clear, so let’s go ahead and create the parameters!

Was this documentation helpful?
YesNo

Leave a Reply

Your email address will not be published. Required fields are marked *