Skip to content

Instantly share code, notes, and snippets.

@rameshelamathi
Created October 4, 2019 15:03
Show Gist options
  • Save rameshelamathi/2366fca51fdc2659ed11c24a2d5e7b1c to your computer and use it in GitHub Desktop.
Save rameshelamathi/2366fca51fdc2659ed11c24a2d5e7b1c to your computer and use it in GitHub Desktop.
Apply a discount rule only for downlodable products. Exclude the rest
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