Last active
August 17, 2020 11:31
-
-
Save Balakrishnan-flycart/97fd79b087bead4d475d644200e9f385 to your computer and use it in GitHub Desktop.
Category and Taxonomy Combination Filter
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(339,340)) && 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(in_array($type, array_keys(\Wdr\App\Helpers\Woocommerce::getCustomProductTaxonomies()))){ | |
| $taxonomies[$type] = isset($filter->value) ? $filter->value : array(); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| 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