I want to disable Cash on Delivery(COD) for some products on my online shopping site.
Is it Possible?
To do this you have to use woocommerce_available_payment_gateways filter.
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 = [12, 34];
$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;
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Please Note: I have't tested this code.