Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active September 14, 2022 11:31
Show Gist options
  • Save danielbitzer/dcb56e40adc0cac9cfb5ca239be18c75 to your computer and use it in GitHub Desktop.
Save danielbitzer/dcb56e40adc0cac9cfb5ca239be18c75 to your computer and use it in GitHub Desktop.
AutomateWoo - Filtering the value of a variable
<?php
/**
* An example of how to filtering the value of a variable
*
* In this example we will format user first and last names.
*/
add_filter( 'automatewoo/variables/after_get_value', 'my_automatewoo_variable_filter_names', 10, 5 );
/**
* @param $value string|false
* @param $data_type string
* @param $data_value string
* @param $parameters array
* @param $workflow AW_Model_Workflow
* @return string
*/
function my_automatewoo_variable_filter_names( $value, $data_type, $data_value, $parameters, $workflow ){
if ( $data_type != 'customer' ) {
return $value;
}
if ( $data_value == 'first_name' || $data_value == 'last_name' ) {
// Format names
$value = ucwords( strtolower( $value ) );
/** @internal $order WC_Order */
if ( $order = $workflow->get_data_item('order') ) {
// if this workflow is order related perform additional logic around billing countries
$auto_fallback_countries = array( 'JP', 'FR' );
$fallback = empty( $parameters['fallback'] ) ? false : $parameters['fallback'];
if ( $fallback && in_array( AutomateWoo\Compat\Order::get_billing_country( $order ), $auto_fallback_countries ) ) {
$value = $fallback;
}
}
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment