禁用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');