php 分层运输,具有多种运费率、选项和价格

szqfcxe2  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(127)

我最近设置了我的分层运输,我读了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,只有免费送货:4和统一费率:9显示。
我应该修改什么以包括免费送货:14,统一费率:15,统一费率:16?
编辑:为了更清楚地说明这一点,我试图做一个3层航运。购物车总额低于200,购物车总额超过200但低于900,购物车总额超过900。不同的费率对应于不同的航运选项/公司。x1c 0d1x
以下是不同的运费参考
·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
b1uwtaje

b1uwtaje1#

在这里,我试图设置所有这些复杂的运费系统在这个代码,因为有很多错误和错误的代码。我已经尽我所能的代码注解。
下面是代码:

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航运设置。

相关问题