Custom AutomateWoo Variables

The following code snippets below are examples of how to create custom AutomateWoo variables. With your own custom variables created, they can then be used in your workflows to further power-up your store.

Note: We are unable to provide support for customizations under our Support Policy. If you need to further customize a snippet, or extend its functionality, we highly recommend Codeable, or a Certified WooExpert.

Dynamically display text based on the number of order items

↑ Back to top

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. These snippets are meant for your theme’s functions.php file, or wherever you prefer to add custom PHP.

<?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;
}
<?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();

Custom date variable for meta field

↑ Back to top

In this example, by extending the class AutomateWoo\Variable_Abstract_Datetime the variable will support the modify and format parameters. These snippets are meant for your theme’s functions.php file, or wherever you prefer to add custom PHP.

<?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;
}
<?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();