0

Based on this code : Update fee dynamically based on radio buttons in Woocommerce checkout I am trying to have a dynamic conditional percentage fee in my checkout page on Woocommerce, based on 3 conditions:

Here is my actual custom code:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

$percentage3 = 0.01;
$fee3 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage3;
$woocommerce->cart->add_fee( 'Fee 3', $fee3, true, '' );

$percentage2 = 0.02;
$fee2 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage2;
$woocommerce->cart->add_fee( 'Fee 2', $fee2, true, '' );

$percentage1 = 0.03;
$fee1 = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage1;
$woocommerce->cart->add_fee( 'Fee 1', $fee1, true, '' );
}

But It doesn't work… How can I make it work like in the linked answer?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Suzan
  • 85
  • 7
  • I have just tested again my answer code on woocommerce last version and it perfectly works on last woocommerce version and the fee change from 3 to 9 when you select the packing option "gift box"… So if it doesn't work for you is that you have some other customizations made by you, your theme or a plugin that are guilty. – LoicTheAztec Jun 30 '18 at 14:24
  • I Remember you kindly that StackOverFlow is not a free coding service. Your question is unclear and undetailed. Your actual code is really incomplete: Where is the radio button details and display code. Where is your jQuery code?. Mostly everything is missing. – LoicTheAztec Jun 30 '18 at 14:42
  • I juste made a fresh installation wordpress and woocommerce as only plugin. I added your code, and the result is the same : `Packaging fee 3,00€` – Suzan Jun 30 '18 at 15:00
  • I have updated [**my answer code to make it work for non logged users**](https://stackoverflow.com/a/49470265/3730754) too. You can see it in action [**HERE on my test web site**](https://www.cbleu.net/sites/wc34/shop/) under last version of woocommerce… add a product to cart and go go checkout… you will see that it works. – LoicTheAztec Jul 02 '18 at 04:24

1 Answers1

0
add_action('woocommerce_cart_calculate_fees', 'add_ship_fee');

function add_ship_fee() {
      $custom_shippingcost = 10;
      WC()->cart->add_fee('VAT: ', $custom_shippingcost);
}

you can add this cost dynamically on cart and checkout. you can also change the cost of variable by getting the cost of cart and add some % of total cost of cart..

Hritik Pandey
  • 899
  • 7
  • 17