Last active
September 14, 2022 11:08
-
-
Save danielbitzer/40a975f2621e44aa38e40c97acd35a25 to your computer and use it in GitHub Desktop.
AutomateWoo - Custom Date Rule Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment