Add fee for certain products in WooCommerce cart

I have a working script that adds a fee for certain products in an array. But it only adds the fee for the first product in the array.

I have tried different options with my knowledge but it doesn't work. Any advice on what i'm doing wrong?

This is the code:

/* Add fee to specific product*/ 
add_action('woocommerce_cart_calculate_fees', 'statie_geld'); 
function statie_geld() { 
   if (is_admin() && !defined('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) {
     $quantiy = $item['quantity']; //get quantity from cart  
     if( in_array( $item['product_id'], statiegeld_ids() )) { 
     WC()->cart->add_fee(__('Statiegeld Petfles 24'), 3.60 * $quantiy ); 
     } 
   } 
} 
function statiegeld_ids() { 
   return array( 4535, 4537, 89694, 89706, 3223, 4742, 14846, 26972, 32925, 32927, 32929, 37475 ); 
} 

Solutions

Your code contains some mistakes

  • No need to use WC()->cart, $cart is passed to the function
  • $quantiy is overwritten on each loop
  • Same for adding the fee, this is overwritten on each loop

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
    // Initialize
    $quantity = 0;
    
    // Loop though each cart item
    foreach ( $cart->get_cart() as $cart_item ) {
        // Compare
        if ( in_array( $cart_item['product_id'], statiegeld_ids() ) ) {
            // Addition
            // Get product quantity in cart  
            $quantity += $cart_item['quantity'];
        }           
    }
    
    // Greater than
    if ( $quantity > 0 ) {
        // Add fee
        $cart->add_fee( __( 'Statiegeld Petfles 24', 'woocommerce' ), 3.60 * $quantity );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

// Specify the product IDs
function statiegeld_ids() { 
   return array( 4535, 4537, 89694, 89706, 3223, 4742, 14846, 26972, 32925, 32927, 32929, 37475 ); 
}

Similar questions

Add more fee when add to cart process Woocommerce plugin
Woocommerce Plugin adds to cart more fee per items. Customer select below in the shopping cart: Product A (Big item) - $300 Product B (Big item) - $500 Product C (Normal item) - $30 Product D (Normal item) - $10 Let's assume the rule for " Big item - Normal item " below: Big item - Charges: $20/item Normal item - Charges: Below $150, Free delivery;...
Don't charge handling fee if only virtual products in cart
With a WooCommerce site, I'm trying to figure out how to stop adding in a handling fee if there are only virtual products in the cart. They're selling virtual gift certificates as well as physical goods, so in the event that there are physical goods in cart with a virtual gift certificate product, they still need to charge handling. But if, say, a ...
Conditionally adding a cart fee if no coupons are applied to cart
In WooCommerce, I am trying to figured out how to add a "Handling Fee" to every order when no coupons or promo codes are applied to cart. Here's my "Fee" or "Handling Charge" code: Any ideas? Thanks
woocommerce add fee to cart based on quantity
I wan to add fee based on quantity. For eg: If quantity in cart = 5 , then the fee to be added should be 4$, If quantity in cart = 7, then the fee to be added should be 8$ I have tried this code to get quantity. Please help with the same.
WooCommerce add a custom fee using ajax to cart totals on checkout page
I am trying make it when user changes the Shipping address select dropdown it dynamically add a fee to cart totals using ajax.I could able to to get the value but when select a another state it wont update the totals. My ajax request: jQuery(document).ready(function () { jQuery('#shipping_state').change(function () { var data ...
Woocommerce won't add my custom fee to the cart total
This is a really odd problem. I am using Woocommerce, and have added an option for users to opt out/add shipping insurance on the checkout page. The checkbox is connected to an AJAX function that does what it is supposed to do. The fee gets added just fine. Every time the cart is changed in any way, the fee is removed, re-calculated, and added agai...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.