Created
January 18, 2019 21:10
-
-
Save mstockton15/3f8d4bab3d6171820a0943a10eeefa89 to your computer and use it in GitHub Desktop.
Add Paypal surcharge/handling fee to Woocommerce cart / checkout
This file contains 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 Paypal 2.9%+0.50 surcharge to Woocommerce cart / checkout | |
function df_add_handling_fee( $cart_object ) { | |
global $woocommerce; | |
$spfee = 0.00; // initialize special fee | |
$spfeeperprod = 0.50; //special fee per product | |
$percentage = 0.029; //percentage | |
//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 ) { | |
$spfee = (($woocommerce->cart->cart_contents_total * $percentage)+($qty * $spfeeperprod)); | |
} | |
endif; | |
} | |
if($spfee > 0 ) { | |
$woocommerce->cart->add_fee( 'Handling Fee', $spfee, true, 'standard' ); | |
} | |
} | |
add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment