Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Balakrishnan-flycart/9567099745cfb4b3beae9d415e6666c7 to your computer and use it in GitHub Desktop.
Save Balakrishnan-flycart/9567099745cfb4b3beae9d415e6666c7 to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Category, Taxonomy(In list) and Tag (Not in list) combinatiom filter (AND)
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(1, 2)) && class_exists('\Wdr\App\Helpers\Woocommerce')) {
$rule_filter = $rule->getFilter();
$product_id = \Wdr\App\Helpers\Woocommerce::getProductId($product);
$product_parent = \Wdr\App\Helpers\Woocommerce::getProductParentId($product_id);
$product_id = !empty($product_parent) ? $product_parent : $product_id;
$categories = \Wdr\App\Helpers\Woocommerce::getProductCategories($product);
$category_id = $product_tag_ids = $tag_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_id = isset($filter->value) ? $filter->value : array();
} elseif ($type == 'product_tags') {
$product_tag_ids = isset($filter->value) ? $filter->value : array();
} else {
if (in_array($type, array_keys(\Wdr\App\Helpers\Woocommerce::getCustomProductTaxonomies()))) {
$taxonomies[$type] = isset($filter->value) ? $filter->value : array();
}
}
}
}
}
if (isset($product_tag_ids) && !empty($product_tag_ids)) {
if (function_exists('wp_get_post_terms')) {
$terms = wp_get_post_terms($product_id, 'product_tag');
if (count($terms) > 0) {
foreach ($terms as $term) {
$tag_ids[] = strval($term->term_id);
}
}
}
$is_product_has_tag = false;
if (!empty($tag_ids)) {
$tag_ids = array_unique($tag_ids);
$is_product_has_tag = array_intersect($tag_ids, $product_tag_ids);
}
if ($is_product_has_tag) {
return false;
}
if (!empty($category_id) && isset($taxonomies) && !empty($taxonomies)) {
$is_product_in_category = count(array_intersect($categories, $category_id)) > 0;
if ($is_product_in_category) {
foreach ($taxonomies as $key => $taxonomy) {
$term_ids = wp_get_post_terms($product_id, $key, array("fields" => "ids"));
$is_product_has_term = count(array_intersect($term_ids, $taxonomy)) > 0;
if (!$is_product_has_term) {
return false;
}
}
if ($is_product_has_term) {
return true;
}
} 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