🤑 Black Friday Sale: 30% OFF for all plans. Coupon: BFD2022
Days
Hours
Minutes
Seconds
GET FREE VERSION GET THE PRO VERSION

How to create custom text variables

Step by step guide of how to create custom text variables.

In this article

Overview

Text variables give great feature to display product specific information ( price, sku, quantity, ... ) inside labels.

Plugin comes packed with a bunch of predefined text variables that will meet almost all needs.

Text variables list

But if you missed some text variable it is also possible to create any default one with any complex logic inside.
All magic is possible with the awl_labels_text_vars filter. It filters all available text variables and gives the option to add any new one.

apply_filters( 'awl_labels_text_vars', (array) $variables );

So code for adding {PRICE} text variable can look like that:

add_filter( 'awl_labels_text_vars', 'my_awl_labels_new_var' );
function my_awl_labels_new_var( $variables ) {
    $variables['/{PRICE}/i'] = array( 
		'func' => 'awl_product_price',
		'desc' => '{PRICE} - Product price',
	);
    return $variables;
}

For the $variables array we need to set a regular expression for text variable as key. In our example it is /{PRICE}/i.
As values we see func and desc array parameters

  • func (string|array) Function that will be called when regular expression is matched for current label text.
  • desc (string) (optional) Text variable description that will be visible inside label edit page under text field.

So for func parameters we need to specify the function name that will be called. This function must return a string that will be used as text variable value. From example below function awl_product_price can looks like that:

function awl_product_price() {
    global $product;
    $price = $product->get_price();
    return $price;
}

So it just returns the current product price. But it is possible to create any logic inside it.

Example

So, for example, we need to create a text variable that will show the price of 10 product items with 20% discount. Inside the shop such label text must looks like

Buy 10 for {10X_PRICE}

where {10X_PRICE} - our new text variable.

So here are the steps to achieve this.

1. Create a custom code snippet and add it somewhere outside the plugins folder.
For example, inside functions.php file of your theme or use any plugin for adding code snippets.

add_filter( 'awl_labels_text_vars', 'my_awl_labels_text_vars' );
function my_awl_labels_text_vars( $variables ) {
    $variables['/{10X_PRICE}/i'] = array( 
		'func' => 'awl_increase_price',
		'desc' => '{10X_PRICE} - Price for 10 items with 20% discount',
	);
    return $variables;
}
 
function awl_increase_price() {
    global $product;
    $price = $product->get_price() * 10 * 0.8;
    return $price;
}

2. Go to the label edit page and find Label text option. Add the following value for it:

Buy 10 for {10X_PRICE}

Label text field

3. That's all! Now we only need to visit the shop page and check our shiny new label with a text variable.

Label with new text variable