禁用WooCommerce中可下载产品的发货结帐字段 [英] Disable shipping checkout fields for downloadable products in WooCommerce

查看:92
本文介绍了禁用WooCommerce中可下载产品的发货结帐字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过在产品提交表单上选中虚拟来自动禁用送货字段,但是默认情况下,我如何在Woocommerce结帐部分中禁用可下载产品的送货字段?

I know that I can automatically disable shipping fields by checking "virtual" on the product submission form, but how could I by default disable shipping fields for downloadable products on Woocommerce checkout section?

推荐答案

您可以使用 WC_Product 条件方法 is_downloadable() 在以下两种情况下定位可下载产品:

You can use the WC_Product conditional method is_downloadable() to target downloadable products in the following 2 cases:

1)。禁用结帐送货字段

1). Disable checkout shipping fields

add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping_address ){
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( $item['data']->is_downloadable() ) {
            $needs_shipping_address = false;
            break; // Stop the loop
        }
    }
    return $needs_shipping_address;
}

2)。完全禁用运输(运输方法和运输字段)

2). Disable shipping completely (shipping methods and shipping fields)

add_filter( 'woocommerce_cart_needs_shipping', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping ){
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( $item['data']->is_downloadable() ) {
            $needs_shipping = false;
            break; // Stop the loop
        }
    }
    return $needs_shipping;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试并可以工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


提醒:在Woocommerce中隐藏运送到其他地址我们只使用:

Reminder: to hide "Ship to a different address" in Woocommerce we just use:

add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');


这篇关于禁用WooCommerce中可下载产品的发货结帐字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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