Note:
This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.
Creating custom rules for AutomateWoo can get complex very quickly, so we provide abstract rule classes for each rule type (string, number, object, select, date) that add helper methods.
Basic rule example
↑ Back to topFor the sake of simplicity, the example below doesn’t use one of the abstract classes. Below is a basic example of creating a rule that checks whether the billing post code of an order matches a supplied value.
This below snippet would go in your functions.php file
<?php
add_filter('automatewoo/rules/includes', 'my_automatewoo_rules' );
/**
* @param array $rules
* @return array
*/
function my_automatewoo_rules( $rules ) {
$rules['my_unique_rule_id'] = dirname(__FILE__) . '/includes/rule.php'; // absolute path to rule
return $rules;
}
Next, you need to create the file the above my_automatewoo_rules
function references. The function above in the example snippet references a rule.php file in the includes folder in your theme. You can modify that to any file path and file name as you’d like, then add the below snippet to create the new rule.
<?php
/**
* In this example we create a rule for order billing post/zip codes.
* This will be a simple true/false string match
*/
class My_AW_Rule_Order_Billing_Post_Code extends AutomateWoo\Rules\Rule {
/** @var string - can be string|number|object|select */
public $type = 'string';
/** @var string - (required) choose which data item this rule will apply to */
public $data_item = 'order';
/**
* Init
*/
function init() {
// the title for your rule
$this->title = __( 'Order Billing Post Code', 'automatewoo' );
// grouping in the admin list
$this->group = __( 'Order', 'automatewoo' );
// compare type is the middle select field in the rule list
// you can define any options here but this is a basic true/false example
$this->compare_types = [
'is' => __( 'is', 'automatewoo' ),
'is_not' => __( 'is not', 'automatewoo' )
];
}
/**
* Validates the rule based on options set by a workflow
* The $data_item passed will already be validated
* @param $data_item
* @param $compare
* @param $expected_value
* @return bool
*/
function validate( $data_item, $compare, $expected_value ) {
// because $this->data_item == 'order' $data_item wil be a WC_Order object
$order = $data_item;
$actual_value = $order->billing_postcode;
// we set 2 compare types
switch ( $compare ) {
case 'is':
return $actual_value == $expected_value;
break;
case 'is_not':
return $actual_value != $expected_value;
break;
}
return false;
}
}
return new My_AW_Rule_Order_Billing_Post_Code(); // must return a new instance
Date rule example
↑ Back to topThe following example shows how a custom field can be used to create a custom date rule. The advantage of creating a custom date rule is you can use AutomateWoo’s existing date based rule features which add more segmenting functionality over a standard custom field rule.
It’s important to note that the format of the date field should be one that PHP can understand.
Again, we start with adding this filter to our theme’s functions.php file.
<?php
/**
* Register the custom rule
*/
add_filter( 'automatewoo/rules/includes', function( $rules ) {
// The date rule must be in a separate PHP file.
include 'includes/delivery-date-rule.php';
// Each rule must have unique name passed as the array key
// The value is the name of the rule class
$rules['custom_order_delivery_date'] = 'Custom_Order_Delivery_Date';
return $rules;
} );
The filter is including a separate file from the earlier mentioned includes folder in your theme named delivery-date-rule.php
. Like before, you can change the file path and name of the file. The below snippet is then the code to create your custom rule around delivery dates.
<?php
defined( 'ABSPATH' ) || exit;
/**
* Custom Rule: Order - Delivery Date
*
* This is an example of creating a custom date rule that uses a meta field as the date value.
* By extending the AutomateWoo\Rules\Abstract_Date class we can use the existing date comparison types with this custom rule.
* For more about date-based rules see: https://automatewoo.com/docs/rules/date-based-rules/
*/
class Custom_Order_Delivery_Date extends \AutomateWoo\Rules\Abstract_Date {
/**
* Specifies the data type used by this rule.
*
* @var string
*/
public $data_item = 'order';
/**
* Custom_Order_Delivery_Date constructor.
*/
public function __construct() {
// If this date is in the past we may want to turn enable past comparison for this rule.
$this->has_is_past_comparision = true;
parent::__construct();
}
/**
* Init the rule.
*/
public function init() {
$this->title = __( 'Order - Delivery Date', 'custom' );
}
/**
* Validates the rule.
*
* @param \WC_Order $order Order we're validating against.
* @param string $compare What variables we're using to compare.
* @param array|null $value Pass this straight to the validate_date method.
*
* @return bool
*/
public function validate( $order, $compare, $value = null ) {
// Get the date from order meta.
$delivery_date = $order->get_meta( 'delivery_date' );
// We need to convert the date from a string to a DateTime object,
// IMPORTANT - this means the date needs to be in a valid format. YYYY-MM-DD is good.
$delivery_date = aw_normalize_date( $delivery_date );
return $this->validate_date( $compare, $value, $delivery_date );
}
}