Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Balakrishnan-flycart/a1ea2e8686b4e4ddc8e686df944f74a0 to your computer and use it in GitHub Desktop.
Save Balakrishnan-flycart/a1ea2e8686b4e4ddc8e686df944f74a0 to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Category and Attribute Filter And Combination
add_filter('advanced_woo_discount_rules_filter_passed', function ($filter_passed, $rule, $product, $sale_badge) {
//'array(1,2)' - give rule id as you needed
if (in_array($rule->getId(), array(407)) && class_exists('\Wdr\App\Helpers\Woocommerce')) {
$rule_filter = $rule->getFilter();
$categories = \Wdr\App\Helpers\Woocommerce::getProductCategories($product);
$category_ids = array();
if ($rule_filter) {
foreach ($rule_filter as $filter) {
$type = isset($filter->type) ? $filter->type : '';
if ($filter->method == 'in_list') {
if ($type == 'product_category') {
$category_ids = isset($filter->value) ? $filter->value : array();
} else if ($type == 'product_attributes') {
$product_attribute_ids = isset($filter->value) ? $filter->value : array();
}
}
}
}
if (!empty($category_ids) && isset($product_attribute_ids) && !empty($product_attribute_ids)) {
$is_product_in_category = count(array_intersect($categories, $category_ids)) > 0;
if ($is_product_in_category) {
$parent_product_id = \Wdr\App\Helpers\Woocommerce::getProductParentId($product);
$attr_ids = array();
$attrs = \Wdr\App\Helpers\Woocommerce::getProductAttributes($product);
if (empty($parent_product_id) && !empty($attrs)) {
foreach ($attrs as $attr) {
$attr_ids = array_merge($attr_ids, \Wdr\App\Helpers\Woocommerce::getAttributeOption($attr));
}
} else {
$variation_id = \Wdr\App\Helpers\Woocommerce::getProductId($product);
$attribute_details = array();
if (!empty($attrs)) {
foreach ($attrs as $key => $attribute_value) {
$attribute_details[$variation_id][$attribute_value] = $key;
}
}
if (isset($attribute_details[$variation_id])) {
foreach ($attribute_details[$variation_id] as $key => $attribute_taxonomy) {
$terms = get_terms(array(
'taxonomy' => $attribute_taxonomy,
'hide_empty' => false,
));
foreach ($terms as $term) {
if ($key == $term->slug) {
$attr_ids[] = $term->term_id;
}
}
}
}
}
$attr_ids = array_unique($attr_ids);
if (count(array_intersect($product_attribute_ids, $attr_ids)) > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
return $filter_passed;
}, 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment