Created
October 21, 2020 05:00
-
-
Save Balakrishnan-flycart/7270b71c47cdd9671ccbd067eb248ebe to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Taxonomies + Attribute Filter (and) 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(1,2,3)' - give rule id as you needed | |
| if(in_array($rule->getId(),array(1,2)) && class_exists('\Wdr\App\Helpers\Woocommerce')){ | |
| $rule_filter = $rule->getFilter(); | |
| $has_filter_passed = array(); | |
| $product_id = \Wdr\App\Helpers\Woocommerce::getProductId($product); | |
| $parent_product_id = \Wdr\App\Helpers\Woocommerce::getProductParentId($product_id); | |
| if($rule_filter){ | |
| foreach($rule_filter as $filter){ | |
| $type = isset($filter->type) ? $filter->type : ''; | |
| if( $filter->method == 'in_list'){ | |
| if ($type == 'product_attributes') { | |
| $product_attribute_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($taxonomies) && !empty($taxonomies) ){ | |
| foreach($taxonomies as $key=>$taxonomy){ | |
| if(!empty($parent_product_id)){ | |
| $term_ids = wp_get_post_terms($parent_product_id, $key, array("fields" => "ids")); | |
| }else{ | |
| $term_ids = wp_get_post_terms($product_id, $key, array("fields" => "ids")); | |
| } | |
| if(!empty($term_ids)){ | |
| $is_product_has_term = count(array_intersect($term_ids, $taxonomy)) > 0; | |
| if($is_product_has_term){ | |
| $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; | |
| } | |
| } | |
| } | |
| } | |
| 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