Disable COD Payment Gateway
-
Help request,
I tried two separate snippets online to disable COD Payment for some products. Both snippet works by disabling the COD payment option in Checkout. However when either snippet is enabled the Appearance/Menus page goes blank. The navigation menu is still visible in customize mode and the home page navigation still works.
I also tried different Themes and Snippet versions, current Version 2.12.0 to no avail. Hopefully someone could point me in the right direction to resolve this issue.
Thanks.
Copy of both snippet used is below.Snippet 1.
——————————————————————–
//Check if product is available in cart or not.
function is_product_in_cart( $prodids ){
$product_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item[‘data’];
if ( in_array( $product->id, $prodids ) ) {
$product_in_cart = true;
}}
return $product_in_cart;
}
// Disable gateway based on product
function payment_gateway_disable_product( $available_gateways ) {
global $woocommerce;
//print_r( $available_gateways );
$prodids=array(1568,1580,1582);
if ( isset( $available_gateways[‘cod’] ) && is_product_in_cart( $prodids ) ) {
unset( $available_gateways[‘cod’] );
}
return $available_gateways;
}
add_filter( ‘woocommerce_available_payment_gateways’, ‘payment_gateway_disable_product’ );Snippet 2.
================================================================
add_filter(‘woocommerce_available_payment_gateways’, ‘show_hide_cod’, 10, 1);function show_hide_cod($gateways)
{
//list of product id to exclude COD
$product_id_list = [1568, 1580, 1582];
$items = WC()->cart->get_cart();
foreach ($items as $item => $values)
{
$_product = $values[‘data’]->post;
if (in_array($_product->ID, $product_id_list))
{
unset($gateways[‘cod’]);
}
}
return $gateways;
}
================================================================The page I need help with: [log in to see the link]
The topic ‘Disable COD Payment Gateway’ is closed to new replies.