[Gelöst] Wie kann ich in WooCommerce die Zahlungsmethode Nachnahme auf bestimmte Produktkategorien beschränken?

  

4
Themenstarter

Ich habe einen WooCommerce e-store und für eine bestimmte Kategorie (E-Books) möchte ich die Zahlungsmethode Nachnahme (cod) entfernen.

Wie kann ich das mit einem Code in functions.php machen?

Danke, Sam!

2 Antworten
3

Hier ist der Code mein Freund (setzen Sie es in functions.php in Ihrem Child Theme):

function custom_payment_gateway_for_product_category($available_gateways) {
    if (is_admin()) return $available_gateways;

    // Define the product category or tag ID
    $target_category = 'example-category'; // Replace with your category slug

    // Get the items in the cart
    foreach (WC()->cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];

        // Check if the product belongs to the target category
        if (has_term($target_category, 'product_cat', $product_id)) {
            // Allow only specific payment gateway(s) and disable others
            unset($available_gateways['cod']); // Disable Cash on Delivery (COD)
            // You can unset other gateways similarly, if needed
        }
    }

    return $available_gateways;
}

add_filter('woocommerce_available_payment_gateways', 'custom_payment_gateway_for_product_category');
1

In WooCommerce >> Einstellungen >> Zahlungen >> Nachnahme >> Deaktivieren Sie Nachnahme akzeptieren, wenn die Bestellung virtuell ist

Kabeljau-Einstellungen
Teilen: