Created
October 4, 2019 15:03
-
-
Save rameshelamathi/2366fca51fdc2659ed11c24a2d5e7b1c to your computer and use it in GitHub Desktop.
Apply a discount rule only for downlodable products. Exclude the rest
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
function woo_discount_rules_exclude_cart_item_from_discount_method($excluded_products, $rule, $variants){ | |
$eligible_rule_id = 9999; // enter the rule ID that applies for downloadable product only | |
if(isset($rule->ID) && $rule->ID == $eligible_rule_id ) { | |
$query = new WC_Product_Query( array( | |
'downloadable' => false, | |
'return' => 'ids', | |
) ); | |
$products = $query->get_products(); | |
if(!empty($products) && is_array($products) && count($products)) { | |
$all_products_to_exclude = array_merge ($excluded_products, $products); | |
foreach($all_products_to_exclude as $product_id) { | |
$product_ids_to_exclude[] = intval($product_id); | |
} | |
$excluded_products = $all_products_to_exclude; | |
} | |
} | |
return $excluded_products; | |
} | |
add_filter('woo_discount_rule_products_to_exclude', 'woo_discount_rules_exclude_cart_item_from_discount_method', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment