如何在Woocommerce中禁用某些特定产品的货到付款 [英] How to disable Cash on Delivery on some specific products in Woocommerce

查看:93
本文介绍了如何在Woocommerce中禁用某些特定产品的货到付款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的在线购物网站上禁用某些商品的货到付款(COD)。

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

有可能吗?

推荐答案


与woocommerce版本3+兼容

Compatible with woocommerce version 3+

基于 停用购物车中是否有特定产品

代码:

add_filter( 'woocommerce_available_payment_gateways', 'conditionally_disable_cod_payment_method', 10, 1);
function conditionally_disable_cod_payment_method( $available_gateways ){
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    // HERE define your Products IDs
    $products_ids = array(37,40,53);

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        // Compatibility with WC 3+
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if (in_array( $cart_item['product_id'], $products_ids ) ){
            unset($available_gateways['cod']);
            break; // As "COD" is removed we stop the loop
        }
    }
    return $available_gateways;
}

代码在您的活动子主题(或活动主题)的functions.php文件中)。

Code goes in functions.php file of your active child theme (or active theme).

经过测试并有效

这篇关于如何在Woocommerce中禁用某些特定产品的货到付款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆