Created
August 27, 2021 07:01
-
-
Save J-H-Mojumder/1aab817689acd73c103bf2a88b2466b2 to your computer and use it in GitHub Desktop.
Hide specific payment gateways for specific vendors or products (subscription pack)
This file contains 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 | |
add_filter('woocommerce_available_payment_gateways', 'show_hide_cod', 10, 1); | |
function show_hide_cod($gateways) { | |
if ( is_page( 'checkout' ) || is_checkout() ){ | |
//list of vendor id to exclude COD | |
$vendor_list = [2]; | |
//subscription type product's ID | |
$subscription_pack_list = [55,54,693,773,37]; | |
$items = WC()->cart->get_cart(); | |
foreach ($items as $item => $values) { | |
$product_id = $values['product_id']; | |
$seller_id = get_post_field( 'post_author', $product_id); | |
//To hide a specific payment method of a specific vendor. | |
if (in_array($seller_id, $vendor_list)) { | |
//unset the payment gateways you want to hide | |
unset($gateways['bacs']); | |
unset($gateways['cod']); | |
unset($gateways['dokan-stripe-connect']); | |
unset($gateways['paypal']); | |
} | |
//To hide a payment gateway for subscription packs. | |
if (in_array($product_id,$subscription_pack_list)) { | |
//unset the payment | |
unset($gateways['bacs']); | |
unset($gateways['cod']); | |
unset($gateways['paypal']); | |
} | |
} | |
} | |
return $gateways; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment