add_action('woocommerce_before_cart', 'apply_coupon_based_on_category');
function apply_coupon_based_on_category() {
    $coupon_code = 'CATEGORYCODE'; // Gutscheincode für bestimmte Kategorie
    $target_category = 'sale'; // Name der Kategorie

    $cart = WC()->cart;
    $coupon_applied = false;

    foreach ($cart->get_cart() as $cart_item) {
        $product = $cart_item['data'];
        if (has_term($target_category, 'product_cat', $product->get_id())) {
            $coupon_applied = true;
            break;
        }
    }

    if (!$coupon_applied) {
        $cart->apply_coupon($coupon_code);
    }
}