【问题标题】:Tiered Shipping with multiple Shipping rates options and prices具有多种运费选项和价格的分层运输
【发布时间】:2017-07-05 14:10:17
【问题描述】:

我最近设置了分层运输,并阅读了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。 不同的费率对应于不同的运输选项/公司。

这里是不同的运费参考

• 200 岁以下的购物车

- Fedex Ground (Free)       => free_shipping:14
- Fedex 2 days ($20)        => flat_rate:15
- Fedex Stand Overnight ($45)   => flat_rate:16

• 购物车低于 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

• 购物车介于 200 和(低于)900 之间

- Fedex Stand 2 days (Free) =>  free_shipping:18

• 购物车多达 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

【问题讨论】:

  • 能否请您添加本教程的链接...与您的代码相关的确实很奇怪,因为之前没有if 的多个( isset( $rates['free_shipping:xx'] ) ) ;( isset( $rates['free_shipping:xx'] ) )... 我不明白是什么你试图在你的代码中做,你肯定犯了很多错误......
  • businessbloomer.com/… 这是教程。我只是想让它工作,我对 PHP 没有任何经验。试图在 woocommerce 中进行分层运输。将有 3 层,每层都有很多运输选项。

标签: php wordpress woocommerce checkout shipping


【解决方案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运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式。

【讨论】:

  • 谢谢,你是最棒的。一切都很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 2022-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多