Skip to content

Instantly share code, notes, and snippets.

@mstockton15
Created January 18, 2019 21:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstockton15/3f8d4bab3d6171820a0943a10eeefa89 to your computer and use it in GitHub Desktop.
Save mstockton15/3f8d4bab3d6171820a0943a10eeefa89 to your computer and use it in GitHub Desktop.
Add Paypal surcharge/handling fee to Woocommerce cart / checkout
//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