Skip to content

Instantly share code, notes, and snippets.

@bmoredrew
Created March 3, 2015 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoredrew/c32ef178f138c8335ca2 to your computer and use it in GitHub Desktop.
Save bmoredrew/c32ef178f138c8335ca2 to your computer and use it in GitHub Desktop.
Add service fee to woo cart per item
add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' );
function df_add_handling_fee( $cart_object ) {
global $woocommerce;
// $specialfeecat = 3711; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 2.50; //special fee per product
//Getting Cart Contents.
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity
foreach($cart as $cart_val => $cid){
$qty += $cid['quantity'];
}
foreach ( $cart_object->cart_contents as $key => $value ) {
$proid = $value['product_id']; //get the product id from cart
//$quantiy = $value['quantity']; //get quantity from cart
$itmprice = $value['data']->price; //get product price
$terms = get_the_terms( $proid, 'product_cat' ); //get taxonamy of the prducts
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
//$catid = $term->term_id;
//if($specialfeecat == $catid ) {
$spfee = $qty * $spfeeperprod;
//}
}
endif;
}
if($spfee > 0 ) {
$woocommerce->cart->add_fee( 'Service Fee', $spfee, true, 'standard' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment