1. Documentation /
  2. AutomateWoo /
  3. Variables /
  4. Custom Variables

Custom Variables

This is a Developer level doc. If you are unfamiliar with code and resolving potential conflicts, select a WooExpert or Developer for assistance. We are unable to provide support for customizations under our  Support Policy.

The following code is an example of how to create a custom AutomateWoo variable. This example looks at a way that you could dynamically display text based on the number of items in an order. E.g. item or items.

functions.php content:
<?php
/**
 * Add the custom variable to the list
 */
add_filter( 'automatewoo/variables', 'my_automatewoo_variables' );

/**
 * @param $variables array
 * @return array
 */
function my_automatewoo_variables( $variables ) {
	// variable's string form is set here, it will be order.pluralize
	$variables['order']['pluralize'] = dirname(__FILE__) . '/variable-order-pluralize.php';
	return $variables;
}


variable-order-pluralize.php content:
<?php

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * @class AW_Variable_Order_Pluralize
 */
class My_AutomateWoo_Variable_Order_Pluralize extends AutomateWoo\Variable {

	/** @var bool - whether to allow setting a fallback value for this variable  */
	public $use_fallback = false;

	
	public function load_admin_details() {
		$this->description = __( "Display text dynamically based on the number of items in an order.", 'automatewoo');
		
		// setting parameters fields is optional
		$this->add_parameter_text_field( 'single', __( 'Used when there is one item purchased.', 'your-text-domain' ) );
		$this->add_parameter_text_field( 'plural', __( 'Used when there are more than one item purchased.', 'your-text-domain' ) );
	}

	/**
	 * @param $order WC_Order
	 * @param $parameters array
	 * @return string
	 */
	public function get_value( $order, $parameters ) {
		$single = isset( $parameters['single'] ) ? $parameters['single'] : '';
		$plural = isset( $parameters['plural'] ) ? $parameters['plural'] : '';

		return $order->get_item_count() == 1 ? $single : $plural;
	}

}

return new My_AutomateWoo_Variable_Order_Pluralize();


View on Github

Example – Custom date variable for meta field

In this example by extending the class AutomateWoo\Variable_Abstract_Datetime the variable will support the modify and format parameters.

functions.php content:
<?php

add_filter( 'automatewoo/variables', 'my_automatewoo_variables' );

/**
 * @param $variables array
 * @return array
 */
function my_automatewoo_variables( $variables ) {
	$variables['order']['my_custom_date'] = dirname(__FILE__) . '/variable-my-custom-date.php';
	return $variables;
}


variable-my-custom-date.php content:
<?php

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * @class My_AW_Variable_Custom_Date
 */
class My_AW_Variable_Custom_Date extends AutomateWoo\Variable_Abstract_Datetime {

	protected $name = 'order.custom_date';

	
	function load_admin_details() {
		$this->description = __( '...', 'automatewoo');
		parent::load_admin_details();
	}


	/**
	 * @param $order WC_Order
	 * @param $parameters array
	 * @return string|false
	 */
	function get_value( $order, $parameters ) {
		$timestamp = $order->get_meta( '_my_custom_timestamp' );
		if ( ! $timestamp ) {
			return false;
		}

		$date = new DateTime();
		$date->setTimestamp( $timestamp );

		return $this->format_datetime( $date, $parameters );
	}
}

return new My_AW_Variable_Custom_Date();

View on Github