• Resolved pcawilliams

    (@pcawilliams)


    Hello,

    I’ve been reading through tutorials but I’m not quite able to figure this problem out. We use Paypal to accept payments and would like to charge a 3% fee for orders that are $5,000 and over.

    Currently I have it setup so that payment is not accepted for orders over a certain limit because we want to make sure that this fee is in place first.

    Can someone explain to me what would need to be added in a Snippet?

    I’ve been reading through this:
    https://awhitepixel.com/blog/woocommerce-checkout-add-custom-fees/

    The following are what is at the link. I need to charge a fee for order totals(including shipping and tax) over $5k and I need the fee to be 3% of the total.

    add_action('woocommerce_cart_calculate_fees', function() {
    	if (is_admin() && !defined('DOING_AJAX')) {
    		return;
    	}
     
    	$cart_total = WC()->cart->get_cart_contents_total();  // This is excluding shipping
    	if ($cart_total < 500) {
    		WC()->cart->add_fee(__('Fee for small transactions', 'txtdomain'), 50);
    	}
    });
    add_action('woocommerce_cart_calculate_fees', function() {
    	if (is_admin() && !defined('DOING_AJAX')) {
    		return;
    	}
    	
    	$percentage = 0.05;  // Percentage (5%) in float
    	$percentage_fee = (WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total()) * $percentage;
     
    	WC()->cart->add_fee(__('A small fee', 'txtdomain'), $percentage_fee);
    });

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there 👋

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Zworthkey

    (@zworthkey)

    hey you can use this peice of code

    function zwk_add_fee_line( $cart ) {
    if($cart->cart_contents_total> 5000){
    $fee = $cart->subtotal * 0.03;
    $cart->add_fee( __( ‘A small fee’, ‘yourtext-domain’ ) , +$fee );
    }

    }
    add_action( ‘woocommerce_cart_calculate_fees’, ‘zwk_add_fee_line’ );

    thanks and regards

    Thread Starter pcawilliams

    (@pcawilliams)

    @zworthkey

    I got this warning:

    Don't Panic
    The code snippet you are trying to save produced a fatal error on line 73:
    
    is_readable(): open_basedir restriction in effect. File(/dev/urandom) is not within the allowed path(s): (/tmp:/home/pacificmarine.net/)
    The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
    
    Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.

    Thoughts?

    Thread Starter pcawilliams

    (@pcawilliams)

    I was able to figure it out.

    The code below will add the cart, shipping and tax totals together and add a 3% fee if it is over $5,000.00

    add_action('woocommerce_cart_calculate_fees', function() {
    	if (is_admin() && !defined('DOING_AJAX')) {
    		return;
    	}
    	
    	$percentage = 0.03;  // Percentage (3%) in float
    	$percentage_fee = (WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total() + WC()->cart->get_taxes_total()) * $percentage;
    
    $cart_total = (WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total() + WC()->cart->get_taxes_total());
    	if ($cart_total > 5000) {
     
    	WC()->cart->add_fee(__('A small fee for orders over $5,000.00', 'txtdomain'), $percentage_fee);
    }
    });
    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    That’s some wonderful news, @pcawilliams! 🎉

    Glad to know that you could figure it out and thank you for sharing the solution with the WooCommerce Community 🙂

    Great! I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    Cheers.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add Percentage as Fee for order over $5k’ is closed to new replies.