【发布时间】:2021-08-13 12:57:23
【问题描述】:
我正在为 WooCommerce 插件使用免费产品样品,并尝试仅为样品产品设置有条件的免费送货,样品产品成本为 0,因此我试图隐藏免费送货,除非订单小计金额为 0。
简而言之:
我只想为样品产品提供免费送货服务,并且免费送货方式应该只对小计 = 0 的产品可见
进一步说明的屏幕截图:
我尝试的代码:
function ts_hide_shipping_for_order_total( $rates ) {
$rate = array();
$order_total = WC()->cart->get_subtotal();
if( $order_total > 0 ) {
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping:20' === $rate->get_method_id() ) {
$rate[ $rate_id ] = $rate;
}
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_for_order_total', 100 );
或
add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 );
function hide_shipping_except_local_when_free_is_available($rates) {
$free = $local = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping:20' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
return ! empty( $free ) ? array_merge( $free, $local ) : $rates;
}
但是这两个代码都没有给我想要的结果。谁能指出我正确的方向?
【问题讨论】:
标签: php woocommerce cart subtotal shipping-method