Created
May 15, 2014 17:19
-
-
Save raphaelchaib/7cc9cec54ec5cbcd216a to your computer and use it in GitHub Desktop.
WooCommerce: Filter gateways by product ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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