• Resolved rrosenthal

    (@rrosenthal)


    I working on a site that applies flat rate shipping to all orders. I would like to override the flat rate to $0 based on user and cart amount.

    I found a code snippet from Byrce Adams that works, except for the conditional code I added — I’m hoping someone can access the code snippet below (added to functions.php) and advise how best to get it working:

    // Free retail shipping

    function adjust_shipping_rate( $rates ) {

    // End if user is logged in
    if ( is_user_logged_in() ) {
    return;
    } else {

    // Check guest shopper cart value before taxes
    if ( $woocommerce->cart->subtotal_ex_tax >= 50 ) {

    // Loop through each rate
    foreach ( $rates as $rate ) {

    // Store the previous cost in $cost
    $cost = $rate->cost;

    // Adjust the cost as needed
    $rate->cost = 0;

    }

    return $rates;

    }}}
    add_filter( ‘woocommerce_package_rates’, ‘adjust_shipping_rate’, 50, 1 );

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Change:

    $woocommerce->cart->subtotal_ex_tax

    To:

    WC()->cart->subtotal_ex_tax
    Thread Starter rrosenthal

    (@rrosenthal)

    Thanks Mike Jolley, the change has me a step closer. The remaining issue is that I need the function to default to flat rate shipping (already setup in WC) when the conditions are not met. In other words, either of these fail (below), then flat rate shipping applies:

    if ( is_user_logged_in() ) {
    return;
    } else {

    if ( WC()->cart->subtotal_ex_tax < 50 ) {
    return;

    I think what’s happening is that when either of the above fails, WC doesn’t know what shipping to apply — and this message displays:

    “Please continue to the checkout and enter your full address to see if there are any available shipping methods.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I think you just need to return $rates; instead of returning nothing.

    Thread Starter rrosenthal

    (@rrosenthal)

    That does it Mike. Thanks so much.

    I am not a PHP coder, but know just enough to be dangerous. I had actually tried the return $rates; in previous attempts, but since the conditional logic wasn’t working, I couldn’t tell if the return $rates; was either.

    For anyone else seeking a conditional overrride to flat rate shipping, where a guest shopper and a cart value of at least $50 gets free shipping, the following should work:

    // Free retail shipping

    function adjust_shipping_rate( $rates ) {

    if ( is_user_logged_in() ) {
    return $rates;
    } else {

    if ( WC()->cart->subtotal_ex_tax < 50 ) {
    return $rates;
    } else {

    // Loop through each rate
    foreach ( $rates as $rate ) {

    // Store the previous cost in $cost
    $cost = $rate->cost;

    // Adjust the cost as needed
    $rate->cost = 0;

    }

    return $rates;

    }}}
    add_filter( ‘woocommerce_package_rates’, ‘adjust_shipping_rate’, 50, 1 );

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Woocommerce override flat rate shipping’ is closed to new replies.