我最近设置了分层发货,我读了this tutorial这个,我修改了他的代码就像这样:
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 10, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
$thresholdsmall = 200;
$thresholdbig = 899.99;
if ( WC()->cart->subtotal < $thresholdsmall ) {
if ( isset( $rates['free_shipping:4'] ) ) unset( $rates['free_shipping:18'] );
if ( isset( $rates['free_shipping:14'] ) ) unset( $rates['free_shipping:19'] );
if ( isset( $rates['free_shipping:14'] ) ) unset( $rates['free_shipping:21'] ) ;
if ( isset( $rates['flat_rate:9'] ) ) unset( $rates['flat_rate:23'] );
if ( isset( $rates['flat_rate:15'] ) ) unset( $rates['flat_rate:24'] );
if ( isset( $rates['flat_rate:16'] ) ) unset( $rates['flat_rate:26'] );
if ( isset( $rates['flat_rate:16'] ) ) unset( $rates['flat_rate:22'] );
if ( isset( $rates['flat_rate:16'] ) ) unset( $rates['flat_rate:25'] );
}
if ( WC()->cart->subtotal > $thresholdbig ) {
( isset( $rates['free_shipping:19'] ) ) ;
( isset( $rates['free_shipping:21'] ) ) ;
unset( $rates['free_shipping:18'] );
unset( $rates['free_shipping:14'] );
unset( $rates['free_shipping:4'] );
( isset( $rates['flat_rate:25'] ) ) ;
( isset( $rates['flat_rate:26'] ) ) ;
unset( $rates['flat_rate:22'] );
unset( $rates['flat_rate:23'] );
unset( $rates['flat_rate:24'] );
unset( $rates['flat_rate:9'] );
unset( $rates['flat_rate:15'] );
unset( $rates['flat_rate:16'] );
}
else {
( isset( $rates['free_shipping:4'] ) ) ;
( isset( $rates['free_shipping:18'] ) ) ;
unset( $rates['free_shipping:19'] );
unset( $rates['free_shipping:21'] );
unset( $rates['free_shipping:14'] );
( isset( $rates['flat_rate:9'] ) ) ;
( isset( $rates['flat_rate:24'] ) ) ;
( isset( $rates['flat_rate:23'] ) ) ;
( isset( $rates['flat_rate:22'] ) ) ;
unset( $rates['flat_rate:15'] );
unset( $rates['flat_rate:16'] );
unset( $rates['flat_rate:25'] );
unset( $rates['flat_rate:26'] );
}
return $rates;
}
现在当我的购物车低于200时,只有free_shipping:4和flat_rate:9正在显示。
我应该修改哪些内容以包含free_shipping:14,flat_rate:15,flat_rate:16?
编辑:为了使这个更清楚,我尝试进行3层运输。购物车总数不到200,购物车总数超过200但不到900,购物车总数超过900。 不同的费率对应不同的运输选项/公司。
以下是不同的运费参考
•CART UNDER 200
- Fedex Ground (Free) => free_shipping:14
- Fedex 2 days ($20) => flat_rate:15
- Fedex Stand Overnight ($45) => flat_rate:16
•CART 900以下
- USPS Priority (free) => free_shipping:4
- USPS Express ($45) => flat_rate:9
- Fedex 2 days AM ($20) => flat_rate:22
- Fedex Stand Overnight ($40) => flat_rate:23
- Fedex Pty. Overnight ($50) => flat_rate:24
•CART BETWEEN 200和(under)900
- Fedex Stand 2 days (Free) => free_shipping:18
•CART最高900
- USPS Express (free) => free_shipping:19
- Fedex Stand. Overnight (Free) => free_shipping:21
- Fedex Pty. Overnight ($20) => flat_rate:25
- Fedex Pty. Saturday Deliv. ($40) => flat_rate:26
答案 0 :(得分:1)
在这里,我尝试在此代码中设置所有这些复杂的运费系统,因为代码中存在许多错误和错误。我尽我所能地评论了代码。
以下是代码:
add_filter( 'woocommerce_package_rates', 'shipping_rates_based_on_cart_amount', 10, 2 );
function shipping_rates_based_on_cart_amount( $rates, $package ) {
if ( WC()->cart->subtotal < 900 ) { ## Under 900
unset( $rates['free_shipping:19'] ); // remove: USPS Express (free)
unset( $rates['free_shipping:21'] ); // remove: Fedex Stand. Overnight (Free)
unset( $rates['flat_rate:25'] ); // remove: Fedex Pty. Overnight ($20)
unset( $rates['flat_rate:26'] ); // remove: Fedex Pty. Saturday Deliv. ($40)
if ( WC()->cart->subtotal < 200 ) { ## Under 200
// For => "Fedex Stand Overnight ($45)"
if ( isset( $rates['flat_rate:16'] ) )
{
unset( $rates['flat_rate:23'] ); // remove: Fedex Stand Overnight ($40)
unset( $rates['flat_rate:24'] ); // remove: Fedex Pty. Overnight ($50)
}
// For => "Fedex 2 days ($20) "
if ( isset( $rates['flat_rate:15'] ) )
unset( $rates['flat_rate:22'] ); // remove: Fedex 2 days AM ($20)
} else { ## Between 200 and under 900
// For => "Fedex Stand 2 days (free)"
if ( isset( $rates['free_shipping:18'] ) )
{
unset( $rates['free_shipping:14'] ); // Fedex Ground (Free)
unset( $rates['flat_rate:15'] ); // remove: Fedex 2 days ($20)
}
// For => "Fedex Stand Overnight ($40)"
if ( isset( $rates['flat_rate:23'] ) )
unset( $rates['flat_rate:16'] ); // remove: Fedex Stand Overnight ($45)
}
} else { ## From 900 (up to 900)
## 1) FEDEX
// For => "Fedex Stand. Overnight (Free)"
if ( isset( $rates['free_shipping:21'] ) )
{
unset( $rates['free_shipping:18'] ); // remove: Fedex Stand 2 days (Free)
unset( $rates['free_shipping:14'] ); // remove: Fedex Ground (Free)
}
// For => "Fedex Pty. Overnight ($20)"
// Or for => "Fedex Pty. Saturday Deliv. ($40)"
if ( isset( $rates['flat_rate:25'] ) || isset( $rates['flat_rate:26'] ) )
{
unset( $rates['flat_rate:15'] ); // remove: Fedex 2 days ($20)
unset( $rates['flat_rate:22'] ); // remove: Fedex 2 days AM ($20
unset( $rates['flat_rate:16'] ); // remove: Fedex Stand Overnight ($45)
unset( $rates['flat_rate:23'] ); // remove: Fedex Stand Overnight ($40)
unset( $rates['flat_rate:24'] ); // remove: Fedex Pty. Overnight ($50)
unset( $rates['flat_rate:9'] ); // remove: USPS Express ($45)
}
## 2) USPS
if ( isset( $rates['free_shipping:19'] ) ) // For => "USPS Express (free)"
unset( $rates['free_shipping:4'] ); // remove: USPS Priority (free)
}
return $rates;
}
此代码应该有效......
您需要刷新送货缓存数据:禁用,保存并启用,保存当前送货区域的相关送货方式,在woocommerce送货设置中。