This two-file example registers {{ order.pluralize }}. Supply single and plural parameters in the workflow variable.
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_plural_order_variable' );
/**
* Register the order.pluralize variable.
*
* @param array $variables Registered variables.
* @return array
*/
function my_store_register_plural_order_variable( $variables ) {
$variables['order']['pluralize'] = __DIR__ . '/class-my-store-variable-order-pluralize.php';
return $variables;
}
class-my-store-variable-order-pluralize.php
<?php
defined( 'ABSPATH' ) || exit;
/**
* Singular or plural order variable.
*/
class My_Store_Variable_Order_Pluralize extends AutomateWoo\Variable {
/**
* Do not expose the standard fallback parameter.
*
* @var bool
*/
public $use_fallback = false;
/**
* Load variable details and parameters.
*/
public function load_admin_details() {
$this->description = __( 'Displays text based on the number of order items.', 'my-store' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
$this->add_parameter_text_field( 'single', __( 'Text for one item.', 'my-store' ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
$this->add_parameter_text_field( 'plural', __( 'Text for more than one item.', 'my-store' ) ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
}
/**
* Get the variable value.
*
* @param WC_Order $order Workflow order.
* @param array $parameters Variable parameters.
* @return string
*/
public function get_value( $order, $parameters ) {
$single = isset( $parameters['single'] ) ? $parameters['single'] : '';
$plural = isset( $parameters['plural'] ) ? $parameters['plural'] : '';
return 1 === $order->get_item_count() ? $single : $plural;
}
}
return new My_Store_Variable_Order_Pluralize();