This two-file example registers {{ order.custom_date }} and formats a Unix timestamp stored in _my_custom_timestamp. Put both files in the same custom plugin directory.
Developer note:
Add custom PHP in a small custom plugin or child theme, and test it on a staging site before production. This example requires ongoing maintenance when extension APIs change.
plugin.php
<?php
add_filter( 'automatewoo/variables', 'my_store_register_automatewoo_variables' );
/**
* Register custom AutomateWoo variables.
*
* @param array $variables Registered variables.
* @return array
*/
function my_store_register_automatewoo_variables( $variables ) {
$variables['order']['custom_date'] = __DIR__ . '/class-my-store-variable-order-custom-date.php';
return $variables;
}
class-my-store-variable-order-custom-date.php
<?php
defined( 'ABSPATH' ) || exit;
/**
* Custom AutomateWoo order date variable.
*/
class My_Store_Variable_Order_Custom_Date extends AutomateWoo\Variable_Abstract_Datetime {
/**
* Load variable details.
*/
public function load_admin_details() {
$this->description = __( 'Displays the custom date stored on the order.', 'my-store' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
parent::load_admin_details();
}
/**
* Get the variable value.
*
* @param WC_Order $order Workflow order.
* @param array $parameters Variable parameters.
* @return string
*/
public function get_value( $order, $parameters ) {
$timestamp = absint( $order->get_meta( '_my_custom_timestamp' ) );
return $timestamp ? $this->format_datetime( $timestamp, $parameters, true ) : '';
}
}
return new My_Store_Variable_Order_Custom_Date();