---
title: "Custom variables in WC - APG SMS Notifications"
description: "Probably the most recurrent query we get about the use of the plugin for WooCommerce from APG WC - APG SMS Notifications is about how the custom variables are used. That is why today we have decided to write this"
url: https://artprojectgroup.com/custom-variables-in-wc-apg-sms-notifications
date: 2017-12-20
modified: 2023-03-22
author: "Art Project Group"
image: https://artprojectgroup.com/wp-content/uploads/2023/02/wc-apg-notificaciones-sms-blog.webp
categories: ["Tutorials"]
tags: ["functions.php", "PHP", "WC - APG SMS Notifications", "WooCommerce plugins"]
type: post
lang: en-US
---

# Custom variables in WC - APG SMS Notifications

[WC - APG SMS Notifications. version 2.13.1.1](https://artprojectgroup.com/wp-content/uploads/2017/12/wc-apg-sms-notifications-version-2-13-1-1.jpg)

Probably the most recurrent query we get about the use of the plugin for WooCommerce from **APG** [**WC - APG SMS Notifications**](/woocommerce-plugins/wc-apg-sms-notifications) is about how the custom variables are used. That is why today we have decided to write this little tutorial to explain the use of the custom variables in the plugin and to see whether its users, and the many who ask us the same question over and over again, understand their use in **WC - APG SMS Notifications** better that way.

When we talk about custom variables we are talking about the ability the plugin has to add to the SMS messages any kind of information contained in the order that is not natively accessible by the PHP code the plugin has.

Let us remember that the variables natively supported by the plugin are: *%id%, %order_key%, %billing_first_name%, %billing_last_name%, %billing_company%, %billing_address_1%, %billing_address_2%, %billing_city%, %billing_postcode%, %billing_country%, %billing_state%, %billing_email%, %billing_phone%, %shipping_first_name%, %shipping_last_name%, %shipping_company%, %shipping_address_1%, %shipping_address_2%, %shipping_city%, %shipping_postcode%, %shipping_country%, %shipping_state%, %shipping_method%, %shipping_method_title%, %payment_method%, %payment_method_title%, %order_discount%, %cart_discount%, %order_tax%, %order_shipping%, %order_shipping_tax%, %order_total%, %status%, %prices_include_tax%, %tax_display_cart%, %display_totals_ex_tax%, %display_cart_ex_tax%, %order_date%, %modified_date%, %customer_message%, %customer_note%, %post_status%, %shop_name%, %order_product%* and *%note%*.

> It is worth remembering that the custom variables are added in the Custom variables text field of WC - APG SMS Notifications without including the % symbols before and after them.

## Types of custom variables

To begin with, and to be as precise as possible, we must make it clear that there are two types of custom variables we can use in **WC - APG SMS Notifications**.

On the one hand there are variables that are not native to WooCommerce and that are not contained in the list of variables natively supported by the plugin, but that are easily accessible if you know their internal name.

A very specific example of this kind of variable that is accessible directly by the plugin would be the typical custom field in the billing and/or shipping form, which may have been created by ourselves or by some free or premium plugin for WooCommerce that we have installed in our online shop. For example our plugin for WooCommerce [**WC - APG NIF/CIF/NIE Field**](/woocommerce-plugins/wc-apg-nif-cif-nie-field) adds a NIF field in the billing form that is easily accessible by adding _*billing_nif* in the *Custom variables* field. The value of the _*shipping_nif* field of the shipping form can also be obtained by adding it in the same field.

On the other hand we have the ability to create a custom variable that is not directly accessible from the data contained in the order or that requires additional programming.

> This kind of custom variable does not have to be added beforehand in the Custom variables field.

## Preliminary notions

The first thing we have to bear in mind before tackling this tutorial is that we need to have solid knowledge of PHP and of the native WooCommerce functions.

> If you lack either of the two things it is better that you do not waste your time and that you [contact APG](/contact) to ask for a quotation in keeping with your specific needs.

If you carry on reading this post it is because you have both abilities; congratulations.

Let us see how we can create custom variables in **WC - APG SMS Notifications**.

## Our own custom variable

The best way to illustrate this tutorial is with a practical example where we are going to create a custom variable called *order_product_completo*. In our specific example we are only going to add one custom variable, but you can add as many custom variables as you need.

> This example variable is going to make it possible for us to read all the attributes of a variable product. It is also very useful if you use the premium plugin [Product Add-Ons](https://woocommerce.com/products/product-add-ons/), since it will read the content of all the form fields you have added to the product.

To be able to add one or several custom variables to the plugin from **APG** you only have to create a small PHP function in the *functions.php* file of your child theme or create a small plugin so as to be able to make use of it. It is important to point out that the PHP function has to make use of the custom filter of **WC - APG SMS Notifications** `apg_sms_message`. A completely functional example of the use of a custom variable is this one:

Copy to clipboardSyntax Highlighter/*Custom SMS message*/
function apg_custom_sms_message( $message, $order_number ) {
$product_data = '';
$order = wc_get_order( $order_number );
foreach ( $order->get_items() as $products ) {
$product = $products->get_data();
$product_data .= $product[ 'quantity' ] . " x " . $product[ 'name' ] . "\r\n";
foreach ( $product[ 'meta_data' ] as $current_product ) {
foreach ( $current_product->get_data() as $datum => $value ) {
if ( $datum == 'key' ) {
$product_data .= $value . ": ";
} else if ( $datum == 'value' ) {
$product_data .= $value . "\r\n";
}
}
}
}
$message = str_replace( "%order_product_full%", $product_data, $message );

return $message;
}
add_filter( 'apg_sms_message', 'apg_custom_sms_message', 10, 2 );
Where, whenever the variable *%order_product_completo%* is found in an SMS message, it will be replaced by something similar to this:

quantity x product name

attribute name: attribute

quantity x product name 2

attribute name: attribute

attribute name 2: attribute

…

As you can see, with the necessary knowledge it is very simple to be able to customise the SMS messages you send to your customers or to yourself by adding to them any kind of information contained in the order. Remember that if you lack the knowledge needed to do it, the best thing you can do is to [contact **APG**](/contact) to ask for a quotation that solves your needs.

We hope you find this tutorial useful and we look forward to your notes and comments, but remember before publishing that **APG** does not provide any kind of free support for its plugins.
