Skip to content

Instantly share code, notes, and snippets.

@DevinVinson
Created August 15, 2013 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DevinVinson/6238339 to your computer and use it in GitHub Desktop.
Save DevinVinson/6238339 to your computer and use it in GitHub Desktop.
Function based on this topic http://wordpress.org/support/topic/restrict-payment-options-based-on-product Small changes and fix to get the function to loop through all categories
/**
*
* Filter gateways on specific category ID
*
**/
function filter_gateways($gateways){
$category_ID = '17';
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) {
if($term->term_id == $category_ID){
unset($gateways['paypal']);
unset($gateways['cheque']);
break;
}
}
}
return $gateways;
}
add_filter('woocommerce_available_payment_gateways','filter_gateways');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment