• Resolved ggmacasaet

    (@ggmacasaet)


    Hi,

    I want to limit some of the shipping methods on certain products. Meaning if i setup 3 of the shipping method / service i will need only 1 to show as an option upon checkout on certain products. Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter ggmacasaet

    (@ggmacasaet)

    Hopefully can get a response ??

    Have you looked at using shipping classes?

    Plugin Author Eivin Landa

    (@forsvunnet)

    Hi, I built a code example for this. The following code will limit the allowed rates based on the shipping classes in the cart. This is quite simple logic where if any of the classes are found then only the allowed rates will be shown. Hopefully you can customise this to match your requirements.

    NB! it’s untested so use with caution.

    add_filter('bring_shipping_rates', function( $rates ) {
    	// Rates to allow when matching one of the shipping classes.
    	$limited_rates = [
    		'mail'
    	];
    	// Shipping classes to limit rates for.
    	$shipping_classes = [
    		'shipping_class_1',
    		'shipping_class_2',
    		'shipping_class_3',
    		'etc...',
    	];
    	// Do not limit rates unless criteria is met.
    	$limit_rates = false;
    
    	// Look for items with a shipping class that doesn not allow mail.
    	$items = WC()->cart->get_cart();
    
    	foreach ( $items as $item ) {
    		$shipping_class = $item['data']->get_shipping_class();
    		if ( in_array( $shipping_class, $shipping_classes ) ) {
    			$limit_rates = true;
    			break;
    		}
    	}
    	if ( ! $limit_rates ) {
    		return $rates;
    	}
    	foreach ( $rates as $key => $rate ) {
    		if ( in_array( $rate['bring_product'], $limited_rates ) ) {
    			// Allow specified rates.
    			continue;
    		}
    		// Remove other rates.
    		unset( $rates[ $key ] );
    		break;
    	}
    	return $rates;
    }, 999 );

    ok

    Plugin Author Eivin Landa

    (@forsvunnet)

    Due to no reply I will mark this as resolved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filter Shipping Methods’ is closed to new replies.