Created
          March 3, 2015 18:40 
        
      - 
      
- 
        Save bmoredrew/c32ef178f138c8335ca2 to your computer and use it in GitHub Desktop. 
    Add service fee to woo cart per item
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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