WooCommerce Free Shipping if over 5 pounds

2019-07-20 09:05发布

I'm trying to write a function that will offer free shipping only (remove all other options) if the order is over 5 pounds (80 ounces), but it doesn't work although the code seems correct.

This is what I have:

  // Hide ALL shipping options but FREe when over 80 ounches (5 pounds)
add_filter( 'woocommerce_available_shipping_methods', 'ship_free_if_over_five_pounds' , 10, 1 );

/**
* Hide ALL Shipping option but free if over 5 pounds
*
* @param array $available_methods
*/
function ship_free_if_over_five_pounds( $available_methods ) {
    global $woocommerce;
    $whats_the_weight = $woocommerce->cart->cart_contents_weight;   

    if($whats_the_weight != 80) :

        // Get Free Shipping array into a new array
        $freeshipping = array();
        $freeshipping = $available_methods['free_shipping'];

        // Empty the $available_methods array
        unset( $available_methods );

        // Add Free Shipping back into $avaialble_methods
        $available_methods = array();
        $available_methods[] = $freeshipping;

    endif;

    return $available_methods;
}

Any thoughts?

The code is based on example #19 on this site:
My 25 Best WooCommerce Snippets For WordPress Part 2

2条回答
姐就是有狂的资本
2楼-- · 2019-07-20 09:39

I've made an plugin where you can set the maximum weight for free shipping!

Take an look at: http://wordpress.org/plugins/woocommerce-advanced-free-shipping/

查看更多
Rolldiameter
3楼-- · 2019-07-20 09:47

I know that this is an old question, but I have this recent alternative…

First, in your code "if the order is over 5 pounds (80 ounces)" your if statement should be if($whats_the_weight > 80) instead of !=… But i think that your code is a little outdated if you use WooCommerce 2.6+.

After instead using global $woocommerce; with $woocommerce->cart->cart_contents_weight; you could use: WC()->cart->cart_contents_weight;

I have this much more recent code snippet based on this official thread for WooCommerce 2.6+. You should try it:

add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

function my_hide_shipping_when_free_is_available( $rates ) {

    $cart_weight = WC()->cart->cart_contents_weight; // Cart total weight

    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id && $cart_weight > 80 ) { // <= your weight condition
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}

For WooCommerce 2.5, You should try this:

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

function hide_shipping_when_free_is_available( $rates, $package ) {

    $cart_weight = WC()->cart->cart_contents_weight; // Cart total weight

    // Only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) && $cart_weight > 80 ) { // Here your weight condition

        // To unset a single rate/method, do the following. This example unsets flat_rate shipping
        unset( $rates['flat_rate'] );

        // To unset all methods except for free_shipping, do the following
        $free_shipping          = $rates['free_shipping'];
        $rates                  = array();
        $rates['free_shipping'] = $free_shipping;
    }

    return $rates;
}
查看更多
登录 后发表回答