Created
January 14, 2017 10:42
-
-
Save fieldoffice/160a0c7d4941caf19edd3059650bc8a6 to your computer and use it in GitHub Desktop.
WooCommerce - Add Connection Fee
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
//CONNECTION FEES | |
function add_connection_fee( $cart_object ) { | |
global $woocommerce; | |
$specialfeecat = 17; // category id for the special fee | |
$spfee = 0.00; // initialize special fee | |
$spfeeperprod = 9.99; //special fee per product | |
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 products | |
if ( $terms && ! is_wp_error( $terms ) ) : | |
foreach ( $terms as $term ) { | |
$catid = $term->term_id; | |
if($specialfeecat == $catid ) { | |
//$spfee = $spfee + $itmprice * $quantiy * $spfeeperprod; | |
$spfee = $spfee + $spfeeperprod * $quantiy; | |
} | |
} | |
endif; | |
} | |
if($spfee > 0 ) { | |
$woocommerce->cart->add_fee( 'Connection Charge', $spfee, true, 'standard' ); | |
} | |
} | |
add_action( 'woocommerce_cart_calculate_fees', 'add_connection_fee' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment