Advertisement
CodeDropz

WooCommerce - Add Custom Fee

Oct 28th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. add_action('woocommerce_cart_calculate_fees', 'dnd_add_custom_fees', 999, 1);
  2.  
  3. function dnd_add_custom_fees($cart) {
  4.     if (is_admin() || defined('DOING_AJAX')) {
  5.         return;
  6.     }
  7.  
  8.     $label = get_option('drag_n_drop_cart_fee_label', 'Custom Fee');
  9.     $amount = 0;
  10.  
  11.     // Debug logging to check if this function is triggered
  12.     error_log('Running dnd_add_custom_fees');
  13.  
  14.     $file_uploads = wp_list_pluck($cart->get_cart_contents(), 'dnd-wc-file-upload');
  15.  
  16.     if ($file_uploads && is_array($file_uploads)) {
  17.         foreach ($file_uploads as $files) {
  18.             if ($files && is_array($files) && count($files) > 0) {
  19.                 $amount += 8.85;
  20.             }
  21.         }
  22.     }
  23.  
  24.     if ($amount > 0) {
  25.         foreach ($cart->get_fees() as $key => $fee) {
  26.             if ($fee->name === $label) {
  27.                 unset($cart->get_fees()[$key]);
  28.             }
  29.         }
  30.  
  31.         // Debug logging to confirm the fee amount
  32.         error_log("Adding fee of amount: $amount");
  33.  
  34.         $cart->add_fee($label, $amount);
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement