How to disable Cash on Delivery on some specific products in Woocommerce

前端 未结 2 1868
时光取名叫无心
时光取名叫无心 2021-01-07 15:42

I want to disable Cash on Delivery(COD) for some products on my online shopping site.

Is it Possible?

2条回答
  •  醉酒成梦
    2021-01-07 16:03

    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.

提交回复
热议问题