Application
We are going to create a discount which lets us reduce the price for a user based on whether he already benefited from certain discounts, which we will define in a list.
1. Step 1:
We will first add our discount to the list of rules, using the “wad_get_discounts_conditions” filter.
First, we add the filter:
add_filter( 'wad_get_discounts_conditions', 'add_my_custom_condition', 10 );
Then we add the callback function linked to this filter:
/ **
* This function allows us to add our condition.
*
* @param type $ arrays contains all conditions.
* @return type
* /
function add_my_custom_condition( $arrays ) {
$arrays['has-discount'] = __( 'If Customer has benefited a discount', 'wad' );
return $arrays;
}
By so doing, we have added our “has-discount” discount to the list of rules.
2. Step 2:
The next step is to add the list of values that can be associated with the discount we just created. We do this by using the ‘wad_fields_values_match’ filter.
First, we call the filter:
add_filter('wad_fields_values_match', 'add_available_option_for_my_custom_condition', 10, 3);
Then we call the callback function linked to the filter:
/ **
* This function adds the options that are available for our condition.
* @param array $ arr contains all available options for each condition.
* @param type $ condition contains the condition (or the designation of the discount).
* @param type $ selected_value contains options that have been selected for the condition ???.
* @return type
* /
function add_available_option_for_my_custom_condition( $arr, $condition, $selected_value ) {
$field_name = 'o-discount [rules] [{rule-group}] [{rule-index}] [value]';// the value of the name attribute of the select field
$discounts_title = wad_get_all_discounts_post_title();
$discount_get_select = get_wad_html_select( $field_name . '[]', false, '', $discounts_title, $selected_value, true );// get_wad_html_select allows you to create a select type field with the parameters given to it; the square brackets ([]) mean that it is a multiple field, if they are not put then a single field will be rendered.
$arr['has-discount'] = $discount_get_select;
return $arr;
}
/ **
* This function return all discounts title
* @return array
* /
function wad_get_all_discounts_post_title() {
$all_discounts = wad_get_all_discounts();// we retrieve all discounts. it is a wad's function.
$discounts_title = array();
foreach ( $all_discounts as $key => $value ) {
$discounts_title[ $value->ID ] = $value->post_title;
}
return $discounts_title;
}
3. Step 3:
The next step is to add our operators using the ‘wad_operators_fields_match’ filter. These operators will link our discount to the values we added previously:
add_filter('wad_operators_fields_match', 'add_comparison_operators_for_my_custom_condition', 10, 3);
Then:
/ **
* This function allows us to associate comparison operators to our condition.
*
* @param type $ arrays contains the operators associated with each condition.
* @param type $ condition contains the condition (or the designation of the discount).
* @param type $ selected_value contains the operator currently active.
* @return type
* /
function add_comparison_operators_for_my_custom_condition( $arrays, $condition, $selected_value ) {
$field_name = 'o-discount [rules] [{rule-group}] [{rule-index}] [operator]';
$arrays_operators = array(
'IN' => __( 'IN', 'wad' ),
'NOT IN' => __( 'NOT IN', 'wad' ),
);
$arrays_operators_select = get_wad_html_select( $field_name, false, '', $arrays_operators, $selected_value );// get_wad_html_select allows you to create a select type field with the parameters given to it. it's wad's function.
$arrays['has-discount'] = $arrays_operators_select;
return $arrays;
}
4. Step 4:
Finally, we will put in measures to apply our discount after checking that the rules defined the match. We will use the ‘wad_is_rule_valid’ filter to achieve this:
add_filter('wad_is_rule_valid', 'is_rule_valid', 10, 2);
Then:
/ **
* Description
*
* @param type $ is_valid after having compared the rules defined in the dashboard and the evaluable condition this boolean allows to say if the rule is valid or not.
* @param type $ rule comment contains the rules defined in the dashboard.
* @return type
* /
function is_rule_valid( $is_valid, $rule ) {
$condition = get_all_discounts_customer_has_benefited(); // we recover the discounts from which the customer has benefited and we compare it to the rules defined in the dashboard according to the operators.
if ( 'has-discount' === $rule['condition']) {
$intersection = array_intersect( $rule['value'], $condition );
if (( 'NOT IN' == $rule['operator'] && $rule['value']! == $intersection ) ||
( 'IN' === $rule['operator'] && $rule['value'] === $intersection )) {
$is_valid = true;
}
}
return $is_valid;
}
/ **
* This function recovers all the reductions from which the client has benefited, this will be the evaluable condition that will be used in is_rule_valid function to evaluate our discount.
*
* @return array
* /
function get_all_discounts_customer_has_benefited() {
// Get all customer orders.
$customer_orders = get_posts(
array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'orderby' => 'date',
'order' => 'DESC',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses()),
'post_status' => array( 'wc-processing' ),
)
);
// get a discount that user have benefitted.
$customer_discount_id = array();
foreach ( $customer_orders as $customer_order ) {
$order_data = wc_get_order( $customer_order );// we get the data on each customer's order.
$customer_discount_id[] = get_post_meta( $order_data->get_id(), $key = 'wad_used_discount', true );// get discount that customer has benefited id.
}
return $customer_discount_id;
}
In reference to the example scenario mentioned earlier in this tutorial, we have been able to achieve the equation: A = B.
In this context, the function allows us to recover B (all the discounts from which the user has benefited) via the ‘get_my_evaluable_condition’ function, and A (the condition established in the dashboard by the administrator) via the ‘is_rule_valid’ function using the ‘$rule’ parameter.
Last updated on March 4, 2023