Forked from AshlinRejo/Discount Rules v2: Load attributes in Categories for BXGY
Last active
April 14, 2023 12:20
-
-
Save AnanthFlycart/5c1d29ea318550bc35a90747db9f6d30 to your computer and use it in GitHub Desktop.
Woo Discount Rules: Load Attributes in Categories for BXGY
This file contains 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_category_taxonomies', function($taxonomy){ | |
// Note: For attributes the taxonomy will be start with prefix pa_ | |
// Example: For attribute Color the taxonomy will be pa_color | |
$taxonomy[] = 'pa_color'; | |
return $taxonomy; | |
}, 10); | |
add_filter('advanced_woo_discount_rules_get_product_categories', function ($categories, $product, $variant){ | |
$taxonomies = apply_filters( 'advanced_woo_discount_rules_category_taxonomies', array()); | |
if(is_array($taxonomies) && !empty($taxonomies)){ | |
foreach ($taxonomies as $taxonomy) { | |
if (!empty($variant) && strpos($taxonomy, 'pa_') !== false && method_exists($variant, 'get_attributes')) { | |
foreach ($variant->get_attributes() as $tax => $slug) { | |
$term = get_term_by('slug', $slug, $tax); | |
if ($term && isset($term->term_id)) { | |
$categories[] = $term->term_id; | |
} | |
} | |
} else { | |
$product_id = $product->get_id(); | |
$terms = get_the_terms($product_id, $taxonomy); | |
if (!empty($terms)) { | |
if ((is_object($terms) || is_array($terms))) { | |
if (!empty($terms)) { | |
foreach ($terms as $term) { | |
if (!empty($term->term_id)) { | |
$categories[] = $term->term_id; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
return $categories; | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment