Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active September 14, 2022 11:30
Show Gist options
  • Save danielbitzer/17d136895c68da9c9d1228191187551b to your computer and use it in GitHub Desktop.
Save danielbitzer/17d136895c68da9c9d1228191187551b to your computer and use it in GitHub Desktop.
Custom AutomateWoo Rules
<?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;
}
<?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
@gourgoul
Copy link

i did the same but cannot see my custom rule. where is the php saved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment