Last active
December 23, 2019 06:06
-
-
Save manutheblacker/7a796d58bf3daea5d06dc82825213380 to your computer and use it in GitHub Desktop.
How to add new rules in Conditional Discounts for WooCommerce extension ?
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 | |
//Step 1 : Added of the condition "If customer ZIP code". | |
add_filter( "wad_get_discounts_conditions", "get_discount_rule_conditions" ); | |
function get_discount_rule_conditions($conditions){ | |
$conditions['customer-zip-code'] = __("If customer ZIP code", "wad"); | |
return $conditions; | |
} | |
//Step 2 : Added of operators "Start with" and "Doesn't start with". | |
add_filter( "wad_operators_fields_match", "get_discount_rule_operators", 10, 3 ); | |
function get_discount_rule_operators($operator_fields, $condition, $selected_value){ | |
$selected_value = array( | |
"START WITH" => __("START WITH", "wad"), | |
"NOT START WITH" => __("NOT START WITH", "wad"), | |
); | |
$operator_fields['customer-zip-code'] = $selected_value; | |
return $operator_fields; | |
} | |
//Step 3 : Added of the match field values | |
add_filter( "wad_fields_values_match", "get_discount_rule_values", 10, 3 ); | |
function get_discount_rule_values($fields_values, $condition, $selected_value){ | |
$text_field = '<input type="text" placeholder="'.__('unique value ,', 'wad').'" name="'.'o-discount[rules][{rule-group}][{rule-index}][value]'.'" value="' . $selected_value . '" required>'; | |
$fields_values['customer-zip-code'] = $text_field; | |
return $fields_values; | |
} | |
//Step 4 : Added of evaluable conditions for getting the discounts | |
add_filter('wad_get_evaluable_condition','get_discount_evaluable_conditions',10,3); | |
function get_discount_evaluable_conditions($condition_is_valid, $rule , $product_id){ | |
if ($rule["condition"] == 'customer-zip-code'){ | |
$customer_postcode = wad_get_customer_postcode(); | |
$values = explode(',', $rule['value']); | |
$condition_is_valid = false; | |
$postcode_is_valid = in_array($customer_postcode,$values); | |
if ($rule["operator"] == 'START WITH' && $postcode_is_valid){ | |
$condition_is_valid=true; | |
}elseif($rule["operator"] == 'NOT START WITH' && $postcode_is_valid){ | |
$condition_is_valid=true; | |
} | |
} | |
return $condition_is_valid; | |
} | |
//Step 5 : Added of control to check if discounts rules is valid. | |
add_filter('wad_is_applicable','get_rules_validity',10,3); | |
function get_rules_validity($is_valid, $discount_obj, $product_id){ | |
$is_valid = false; | |
if($discount_obj->rules_verified===true){ | |
$is_valid = true; | |
} | |
return $is_valid; | |
}; | |
/** | |
* This function allow to get the customer postcode who is buying. | |
* | |
* @return string | |
*/ | |
function wad_get_customer_postcode(){ | |
global $woocommerce; | |
$customer_postcode = $woocommerce->customer->get_billing_postcode(); | |
return $customer_postcode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment