Home > Software design >  How to add additional fees to different products in WooCommerce cart
How to add additional fees to different products in WooCommerce cart

Time:11-07

I use this code to add additional fee to a specific product id's. The problem is that I can add only one fee to a different products id's.

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids'); 
function add_fees_on_ids() { 
   if (is_admin() && !defined

('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) { 
     if( in_array( $item['product_id'], 

fee_ids() )) { 
     WC()->cart->add_fee(__('ADDITIONAL FEE:'), 5); 
     } 
   } 
} 
function fee_ids() { 
   return array( 2179 ); 
}

I need to add different fees to different products - for example:

  • Product 1 with product ID 1234 will have "xx" additional fee.
  • Product 2 with product ID 5678 will have "xx" additional fee.

With this code I can set only one fee for a different products. How can I add different fees to different products in WooCommerce?

CodePudding user response:

Hello and thank you all! I found a solution to the problem. I just copy the code described below as many times as I have different amounts for different products and change it:

add_action('woocommerce_cart_calculate_fees', 'add_fees_on_ids'); 
function add_fees_on_ids() { 
   if (is_admin() && !defined

('DOING_AJAX')) {return;} 
   foreach( WC()->cart->get_cart() as $item_keys => $item ) { 
     if( in_array( $item['product_id'], 

fee_ids() )) { 
     WC()->cart->add_fee(__('ADDITIONAL FEE:'), 5); 
     } 
   } 
} 
function fee_ids() { 
   return array( 2179 ); 
}

I change add_fees_on_ids to add_fees_on_ids_1 and fee_ids to fee_ids_1 and ADDITIONAL FEE: to ADDITIONAL FEE 1:

I know that for some it may not look professional but it is a solution to my problem.

CodePudding user response:

There are several ways. For example, you could indeed add a custom field to the admin product settings.

But it doesn't have to be complicated, installing an extra plugin for this is way too far-fetched!

Adding the same code several times is also a bad idea, because that way you will go through the cart several times. Which would not only slows down your website but will also eventually cause errors.

Adding an array where you would not only determine the product ID but also immediately determine an additional fee based on the array key seems to me to be the most simple and effective way.

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Add in the following way: Additional fee => Product ID
    $settings = array(
        10  => 1234,
        20  => 5678,
        5   => 30,
        20  => 813,
        2   => 815,
    );
    
    // Initialize
    $additional_fee = 0;
    
    // Loop through cart contents
    foreach ( $cart->get_cart_contents() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];
        
        // In array, get the key as well
        if ( false !== $key = array_search( $product_id, $settings ) ) {
            $additional_fee  = $key;
        }
    }

    // If greater than 0, so a matching product ID was found in the cart
    if ( $additional_fee > 0 ) {
        // Add additional fee (total)
        $cart->add_fee( __( 'Additional fee', 'woocommerce' ), $additional_fee, false );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

CodePudding user response:

I. You could use the WordPress plugin "Advanced Custom Fields for that:

  1. install the plugin
  2. set a field group with a title such as "Product with fee" and set its location rules to "Post Type" => "is equal to" => "Product"
  3. " Add Field" of "Field Type" "Text" and notice its "Name" (not "Label") for later checks, for example "fee"
  4. Set fee value at the appropriate WooCommerce product posts at that very fee meta field
  5. Within your 'woocommerce_cart_calculate_fees' hook callback loop over cart products and check for products with set values for the meta field named "fee" and add your fee adding code within that condition:
if (get_field( "fee" )) {        // get_field() returns false if empty aka not set from admin area
  // fee adding code for single product in cart
};

II. Or you could achieve that by adding and displaying a WooCommerce custom product meta field as described in depth here:

https://www.cloudways.com/blog/add-custom-product-fields-woocommerce/

  • Related