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

问题描述

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

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

有可能吗?

推荐答案

基于 。

代码:

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中禁用某些特定产品的货到付款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 12:12