• Using woocommerce usps and fedex I have products that are flammable and cannot be shipped by USPS or any FEDEX but ground. No air shipping for flammable. I am trying to write a filter and function to hide USPS and maybe even all FEDEX options other than ground when these products are in my cart at checkout. My site is in dev so I can not include it for you to see. Below is my attempt at the code.

    // Hide USPS shipping methods when flammable items in cart
    add_filter( 'woocommerce_shipping_methods', 'hide_usps_when_flammable_in_cart' , 1 );
    
    /** Hide usps shipping when flammable items in cart
    @param array $available_methods */
    
    function hide_usps_when_flammable_in_cart( $available_methods ) {
    
    global $woocommerce;
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    //store product id's in array
    $flammableitems = array(191,192,194,196,199,201,204,206,208,210,212,214,217,262);
    			if(in_array($values['product_id'],$flammableitems )){
    	unset($available_methods['usps']);
    	break;
    	}
    }
    return $available_methods;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter pathfinder8888

    (@pathfinder8888)

    I think I can code this if I know the correct filter tag to exclude USPS. I have tried everything like ‘woocommerce_shipping_methods’

    Thread Starter pathfinder8888

    (@pathfinder8888)

    OK, so I wrote my first plugin. Kinda overdid it but needed to start writing my own plugins. It is not elegant or very useable but I will work on it when I have time.

    <?php
    /*
    Plugin Name: Steve's Flammable Shipping Plugin
    Plugin URI: https://www.firstcoastingenuity.com/plugins
    Description: This plugin will hook into woocommerce shipping to only show FEDEX Ground rates in the checkout page when the product or category containing volatile products is in cart.
    Author: Steve Jones
    Version: 1.0
    Author URI: https://www.firstcoastingenuity.com
    */
    
    /*  Copyright 2014  Steve Jones  (email : [email protected])
    
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License, version 2, as
        published by the Free Software Foundation.
    
        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
    
        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    		// Hide USPS shipping methods when flammable items in cart spj 02/02/2014
    		add_filter( 'woocommerce_available_shipping_methods', 'hide_usps_when_flammable_in_cart' , 1 );
    
    		/** Hide usps shipping when flammable items in cart
    			@param array $available_methods
    		*/
    		function hide_usps_when_flammable_in_cart( $available_methods ) {
    			 // printing the methods so I know the available methods to exclude
    			 // echo "
    <pre>" . print_r( $available_methods, true ) . "</pre>
    ";
    			global $woocommerce;
    			 // Get all products in cart
    			$products = $woocommerce->cart->get_cart();
    			//store product id's in array
    			$volatile = array(170,192,196,199,204,209,210,214,749,747,743,745,740,738,736,734,732,729,727,725,723,721,718,715,712,709,690,693,697,699,701,703,707,802,806,808,812,814,816,818,820,822,686,684,681,679,676,673,670,184,174,810,706);
    
    			// loop through the items looking for one in the volatile array
    			foreach ( $products as $key => $item ) {
    				if( in_array( $item['product_id'], $volatile ) ) {
    					unset($available_methods['usps:flat_rate_box_priority']);
    					unset($available_methods['usps:flat_rate_box_express']);
    					unset($available_methods['usps:D_PRIORITY_MAIL']);
    					unset($available_methods['usps:D_STANDARD_POST']);
    					unset($available_methods['fedex:FEDEX_EXPRESS_SAVER']);
    					unset($available_methods['fedex:FEDEX_2_DAY_AM']);
    					unset($available_methods['fedex:FIRST_OVERNIGHT']);
    					unset($available_methods['fedex:PRIORITY_OVERNIGHT']);
    					unset($available_methods['fedex:FEDEX_2_DAY']);
    					unset($available_methods['fedex:STANDARD_OVERNIGHT']);
    					break;
    				}
    			}
    
    			return $available_methods;
    		}

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shipping volitile products ground fedex only’ is closed to new replies.