Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Created March 14, 2014 23:33

Revisions

  1. ChromeOrange created this gist Mar 14, 2014.
    35 changes: 35 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * Deactivate Flat Rate Shipping if products with specific shipping
    * classes are in the cart
    *
    * Add the shipping class slugs to the $shippingclass_array array
    */
    add_filter( 'woocommerce_shipping_flat_rate_is_available', 'unset_woocommerce_shipping_methods_flat_rate', 10 ,2 );
    function unset_woocommerce_shipping_methods_flat_rate ( $return, $package ) {

    // Setup an array of shipping classes that do not allow Flat Rate Shipping
    $shippingclass_array = array( 'pickup-only' );

    // loop through the cart checking the shipping classes
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

    $shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );

    if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
    /**
    * If a product in the cart has a shipping class that does not allow for Flat Rate Shipping
    * then return false to unset the Flat Rate shipping method and break, no need to carry on
    */
    return false;
    break;
    }

    }

    /**
    * It we make it this far then
    * Flat Rate must be available, return true
    */
    return true;

    }