Apply discounts only for a specific products which has custom field data (meta)

Apply discounts only to specific products with custom field data (meta).

// Apply discounts only specific products which has custom field data (meta) 
add_filter('advanced_woo_discount_rules_filter_passed', function($filter_passed, $rule, $product) {
    if (is_object($product) && method_exists($product, 'get_id')) {
        $product_id = $product->get_id();
        if (!get_post_meta($product_id, 'your_key', true)) { // here you need to add your custom field's meta key
            return false;
        }
    }
    return $filter_passed;
}, 100,3);

Last updated