• Hi there, I think this option may be useful:

    • Hide Gateways based on specific product ID(s)

    Here is the CODE:

    // Disable Woocommerce payment methods based on specific product ID in the cart
    add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable_gateways_based_on_product_id', 10, 1 );

    function wpsh_disable_gateways_based_on_product_id( $gateways ) {

    // Check if cart is initialized
    if ( ! WC()->cart ) {
    return $gateways;
    }

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

    // Check if the product ID is 145
    if ( $product_id == 145 ) {
    // If the condition is met, disable the payment gateway
    if ( isset( $gateways['bacs'] ) ) {
    unset( $gateways['bacs'] );
    }
    break;
    }
    }

    return $gateways;
    }

    Refards

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Algoritmika

    (@algoritmika)

    Hi, @nadhonar,

    Sure, will do.

    Thread Starter Mo

    (@nadhonar)

    Thanks for your attention; Also may we have hiding gateways based on product categories?

    Yes based on categories is urgently required, is it possible you can you add this quickly?

    Thread Starter Mo

    (@nadhonar)

    The following Code may be useful:

    // Disable Woocommerce payment methods based on product category in the cart
    add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable_gateways_based_on_category', 10, 1 );

    function wpsh_disable_gateways_based_on_category( $gateways ) {

    // Check if cart is initialized and has contents
    if ( ! WC()->cart || WC()->cart->is_empty() ) {
    return $gateways;
    }

    // Define the category ID to check (e.g., 6)
    $category_id_to_check = 6;

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

    // Check if the product belongs to the specific category
    if ( has_term( $category_id_to_check, 'product_cat', $product_id ) ) {
    // If the condition is met, disable the BACS payment gateway
    if ( isset( $gateways['bacs'] ) ) {
    unset( $gateways['bacs'] );
    }
    break; // Exit loop after the condition is met
    }
    }

    return $gateways;
    }

    Regards

    Plugin Author Algoritmika

    (@algoritmika)

    Hi, @nadhonar and @trentcode,

    Sure, please give me a day or so.

    Plugin Author Algoritmika

    (@algoritmika)

    Hi, @nadhonar and @trentcode,

    Please update the plugin to the latest v2.4.0 (released today).

    After the update, you will find the new “Product” and “Product Category” sections (in “WooCommerce > Settings > Conditional Payment Gateways”).

    Please give it a try and let me know what you think.

    P.S. Just in case, I’ve also added these new sections as well: “Product Tag”, “Product Shipping Class” and “Product Taxonomy” (for custom taxonomy, e.g., brands).

    Thread Starter Mo

    (@nadhonar)

    Awesome, Also adding “Product Tag” is really useful.

    Thanks

    Plugin Author Algoritmika

    (@algoritmika)

    Happy to help ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.