I am using the following snippet to add a flat rate handling fee to physical items in my cart. My company offers free books with free shipping to our employees via use of a coupon, so I need this fee to be removed if the cart total is zero. Not sure how to accomplish this.
/* Add handling 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' );
}
I have tried researching the issue and have not found a solution.
New contributor
monkeymays is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.