Skip to content

Instantly share code, notes, and snippets.

@raphaelchaib
Created May 15, 2014 17:19
Show Gist options
  • Select an option

  • Save raphaelchaib/7cc9cec54ec5cbcd216a to your computer and use it in GitHub Desktop.

Select an option

Save raphaelchaib/7cc9cec54ec5cbcd216a to your computer and use it in GitHub Desktop.
WooCommerce: Filter gateways by product ID
<?php
// Filter gateways by product ID
add_filter('woocommerce_available_payment_gateways', 'cg_filter_gateways', 1);
function cg_filter_gateways($gateways) {
global $woocommerce;
$authorized_products = array(1,2,5);
$gateways_to_unset = array('paypal', 'cielo');
$unset_gateways = true;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Store product id's in array
if(in_array($values['product_id'], $authorized_products)){
$unset = false;
}
}
if($unset) {
foreach($gateways_to_unset as $gw) {
unset($gateways[$gw]);
}
}
return $gateways;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment