• Resolved jackofallspades

    (@jackofallspades)


    I have 2 snippets which look identical, but are slightly different. I am using the same function twice, which is why I have this error. How can I combine these two snippets together? I hope you can help!

    #1)

    add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
       
    function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
      
    
     $shipping_class_target = '1173'; // This is Deluxe Tables shipping class ID 
       $in_cart = false;
       foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
          if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
             $in_cart = true;
             break;
          } 
       }
       if ( $in_cart ) {
          unset( $rates['local_pickup:45'] ); // This is Local Pick up shipping method with ID 
       }
       return $rates;
    }

    And #2

    add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
       
    function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
      
       $shipping_class_target = '1750'; // This is Stag & Doe shipping class ID 
       $in_cart = false;
       foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
          if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
             $in_cart = true;
             break;
          } 
       }
       if ( $in_cart ) {
          unset( $rates['WB_Custom_WooCommerce_Shipping_Method52'] ); // This is Delivery shipping method with ID 
       }
       return $rates;
    
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cannot redeclare function Error’ is closed to new replies.