Last active
September 1, 2020 15:00
-
-
Save Balakrishnan-flycart/d4458d5a3df4026670c626abee0072bf to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Category and Tag Filter Combination
This file contains hidden or 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
| add_filter('advanced_woo_discount_rules_filter_passed', function($filter_passed, $rule, $product, $sale_badge){ | |
| //'array(339,340)' - give rule id as you needed | |
| if(in_array($rule->getId(),array(398)) && 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 = 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(); | |
| }else if($type == 'product_tags'){ | |
| $product_tag_ids = isset($filter->value) ? $filter->value : array(); | |
| } | |
| } | |
| } | |
| } | |
| if(!empty($category_id) && isset($product_tag_ids) && !empty($product_tag_ids) ){ | |
| $is_product_in_category = count(array_intersect($categories, $category_id)) > 0; | |
| if($is_product_in_category){ | |
| $tag_ids = array(); | |
| 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 = array_intersect($tag_ids, $product_tag_ids); | |
| if(!$is_product_has_tag){ | |
| return false; | |
| } | |
| 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