WooCommerce: Restrict payment options based on product

A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart. Add it to your theme’s functions.php

/*
 * A simple filter to disable a user-specified payment gateway when a product with a user-specified category is added to the shopping cart
 *  - Note:  If multiple products are added and only one has a matching category, it will remove the payment gateway
 * Requires:
 *    payment_NAME : One of the five hardcoded Woocommerce standard types of payment gateways - paypal, cod, bacs, cheque or mijireh_checkout
 *    category_ID :   The ID of the category for which the gateway above will be removed.  Get the ID by clicking on the category under Products -> Categories and reading the "tag_ID" in the address bar
 *                             i.e. http://ubuntu.humble.lan/wp-admin/edit-tags.php?action=edit&taxonomy=product_cat&tag_ID=20&post_type=product <-- the tag_ID is 20
 * Coded by sean _ at _ techonfoot.com
 * Thanks to boxoft - http://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page
 * Usual free code disclaimer - use at your own risk
 * This code was tested against Woocommerce 2.1.12  and WordPress 3.9.1 
 */
function filter_gateways($gateways){

$payment_NAME = 'dibs'; // <--------------- change this
$category_ID = '15';  // <----------- and this

 global $woocommerce;

 foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    foreach ($terms as $term) {
        // 20 is the ID of the category for which we want to remove the payment gateway
        if($term->term_id == $category_ID){
               unset($gateways[$payment_NAME]);
                   // If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
                    break;
          }
        break;
    }

   }
    return $gateways;

}

add_filter('woocommerce_available_payment_gateways','filter_gateways');

 

 

Advertisements

5 thoughts on “WooCommerce: Restrict payment options based on product

  1. Hi Pinaki,
    very nice work & exactly what I was looking for.
    Thank you so much 🙂

    P.S.: Do you know of a way or plugin how I can set the following option?
    This product can’t be ordered in combination with any other product in the store. Meaning: When you put this item in your cart you can’t add any other items.
    Is this possible?
    best regards
    Josh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s