[Risolto] Come posso limitare il metodo di pagamento in contrassegno a specifiche categorie di prodotti in WooCommerce?

  

4
Argomento iniziale

Ho un negozio elettronico WooCommerce e per una particolare categoria (libri elettronici) voglio rimuovere il metodo di pagamento in contrassegno (cod).

Come posso farlo con un codice in functions.php?

Grazie, Sam!

2 risposte
3

Ecco il codice, amico mio (inseriscilo in functions.php nel tuo tema figlio):

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 >> Impostazioni >> Pagamenti >> Contanti alla consegna Deselezionare Accettare il contrassegno se l'ordine è virtuale

Impostazioni del merluzzo
Condividi: