Tiered Shipping with multiple Shipping rates optio

2019-05-22 22:36发布

I recently set up my tiered shipping and I read this tutorial about this, I modified his code to mine like this:

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;
}

Right now when my cart is under 200, only free_shipping:4 and flat_rate:9 is showing.

What should I modify to include free_shipping:14,flat_rate:15,flat_rate:16?

Edit: To make this clearer I tried to make a 3 tiered shipping. Cart total that is less than 200, cart total that is more than 200 but less than 900, and cart total more than 900. The different rates correspond to the different shipping options/companies. enter image description here

Here are the different shipping rates references

• CART UNDER 200

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

• CART UNDER 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 and (under) 900

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

• CART UP TO 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

1条回答
小情绪 Triste *
2楼-- · 2019-05-22 23:21

Here I have tried to set all this complicated shipping rates system in this code, as there is a lot of errors and mistakes in your code. I have commented the code the best I can.

Here is that code:

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;

}

This code should work…

You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.

查看更多
登录 后发表回答