PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] How to add a percentage based surcharge (plus tax of surcharge) to all transactions

 January 08, 2022     checkout, hook-woocommerce, php, woocommerce, wordpress     No comments   

Issue

I'm trying to add a percentage based fee from Woocommerce subtotal in both cart and checkout. However, have this fee made up with a 5% surcharge of subtotal + 20% tax of the 5% surcharge.

  • Subtotal: £100
  • Surcharge: £6 (<- £5 (5%) + £1 Tax Surchange (20%)

I have managed to add the 5% percentage based fee of Subtotal. However, how do you add the 20% of $surcharge on top?

Also, will this type of change be effective for emails too? (As well as cart / checkout) is

Any advice would be appreciated.

Thanks in advance.

    add_action( 'woocommerce_cart_calculate_fees','custom_adminfee_surcharge' );
    function custom_adminfee_surcharge() {
      global $woocommerce;
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
$percentage = 0.05 * .20;
        $surcharge = ( $woocommerce->cart->subtotal + $woocommerce->cart->shipping_total ) * $percentage;   
        $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
    }

Solution

This should be 5% of the subtotal then 20% of that 5%...

add_action( 'woocommerce_cart_calculate_fees','custom_adminfee_surcharge' );
function custom_adminfee_surcharge() {
  global $woocommerce;
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    $percentage = ( $woocommerce->cart->subtotal + $woocommerce->cart->shipping_total ) * 0.05;
    $surcharge = $percentage * 0.20;
    $total_charges = $percentage + $surcharge;

    $woocommerce->cart->add_fee( 'Surcharge', $total_charges, true, '' );
}


Answered By - Bossman
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing