Created
October 20, 2020 05:30
-
-
Save Balakrishnan-flycart/50234f93b1e2a94a37f25c702535e5b7 to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Tag + 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(419,420)' - give rule id as you needed | |
| if(in_array($rule->getId(),array(419)) && class_exists('\Wdr\App\Helpers\Woocommerce')){ | |
| $rule_filter = $rule->getFilter(); | |
| $product_id = \Wdr\App\Helpers\Woocommerce::getProductId($product); | |
| $parent_product_id = \Wdr\App\Helpers\Woocommerce::getProductParentId($product_id); | |
| $product_id = !empty($parent_product_id) ? $parent_product_id : $product_id; | |
| if($rule_filter){ | |
| foreach($rule_filter as $filter){ | |
| $type = isset($filter->type) ? $filter->type : ''; | |
| if( $filter->method == 'in_list'){ | |
| if($type == 'product_tags'){ | |
| $product_tag_ids = isset($filter->value) ? $filter->value : array(); | |
| }else if ($type == 'product_attributes') { | |
| $product_attribute_ids = isset($filter->value) ? $filter->value : array(); | |
| } | |
| } | |
| } | |
| } | |
| if(isset($product_tag_ids) && isset($product_attribute_ids) && !empty($product_tag_ids) && !empty($product_attribute_ids)){ | |
| $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 = 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){ | |
| $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