With a WooCommerce site, I'm trying to figure out how to stop adding in a handling fee if there are only virtual products in the cart. They're selling virtual gift certificates as well as physical goods, so in the event that there are physical goods in cart with a virtual gift certificate product, they still need to charge handling. But if, say, a customer only has a virtual gift certificate in their cart, they wouldn't be charged handling.
Here's what I have in their functions.php as far as an action to add handling. I just don't know what to put in there to test for only virtual products in the cart.
/* Add handling fee of $3 to cart at checkout */
add_action( 'woocommerce_cart_calculate_fees','handling_fee' );
function handling_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = 3.00;
$woocommerce->cart->add_fee( 'Handling', $fee, false, 'standard' );
}
I figured it out. Here's the code that worked.
/* Add handling fee of $3 to cart at checkout */
add_action( 'woocommerce_cart_calculate_fees','handling_fee' );
function handling_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// First test if all products are virtual. Only add fee if at least one product is physical.
$allproductsvirtual = true;
$fee = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$product = $values['data'];
if( !$product->is_virtual() ){
$allproductsvirtual = false;
}
}
if ( !$allproductsvirtual ) {
$fee = 3.00;
}
$woocommerce->cart->add_fee( 'Handling', $fee, false, 'standard' );
}