Web & Software Developer

WooCommerce – Check if any product in cart has a certain Shipping Class

This is a function for WooCommerce. It checks to see if there is a product with a certain “Shipping Class” in the cart. It takes one parameter, the slug of the Shipping Class. The function returns true if a product with the Shipping Class is found in the cart. Otherwise, it returns false.

/**
 * Check if the cart has product with a certain Shipping Class
 * @param string $slug the shipping class slug to check for
 * @return bool true if a product with the Shipping Class is found in cart
 */
function cart_has_product_with_shipping_class( $slug ) {

	global $woocommerce;

	$product_in_cart = false;

	// start of the loop that fetches the cart items

	foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {

	    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_shipping_class' );

		if ( $terms ) {
			foreach ( $terms as $term ) {
				$_shippingclass = $term->slug;
				if ( $slug === $_shippingclass ) {
					
					// Our Shipping Class is in cart!
					$product_in_cart = true;
				}
			}
		}
	}

	return $product_in_cart;
}


Usage

For example, to check for the Shipping Class ‘free-shipping’, use this:

if ( cart_has_product_with_shipping_class( 'free-shipping' ) ) {

	// Yes, the cart has a product with the 'free-shipping' Shipping Class.

} else {

	// The cart does NOT have a product with the 'free-shipping' Shipping Class.

}

By

We've 4 Responses

  1. January 19th, 2018 at 5:34 am

    My code

    function cart_has_product_with_shipping_class( $slug ) {
     
        global $woocommerce;
     
        $product_in_cart = false;
     
        // start of the loop that fetches the cart items
     
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
     
            $_product = $values['data'];
            $terms = get_the_terms( $_product->id, 'product_shipping_class' );
     
            if ( $terms ) {
                foreach ( $terms as $term ) {
                    $_shippingclass = $term->slug;
                    if ( $slug === $_shippingclass ) {
                         
                        // Our Shipping Class is in cart!
                        $product_in_cart = true;
                    }
                }
            }
        }
     
        return $product_in_cart;
    }
    
    
    function payment_gateway_disable_product( $available_gateways ) {
    global $woocommerce;
    //print_r( $available_gateways );
    if ( cart_has_product_with_shipping_class( 'gift' ) ) {
     
        if ( isset( $available_gateways['ccavenue'] ) && cart_has_product_with_shipping_class( $slug ) ) {
    unset( $available_gateways['cod'] );
    }else{
    unset( $available_gateways['cod'] );
    }
    return $available_gateways;
     
    }
    }
    add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_product' );
    
    avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]