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,
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 );
Optional:
To display the addition per fee separately, so that the customer knows which fee refers to which product, you can use a multidimensional array.
Add the necessary information to the settings array, everything else happens automatically.
So you get:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Settings
$settings = array(
array(
'product_id' => 30,
'amount' => 5,
'name' => __( 'Additional service fee', 'woocommerce' ),
),
array(
'product_id' => 813,
'amount' => 10,
'name' => __( 'Packing fee', 'woocommerce' ),
),
array(
'product_id' => 815,
'amount' => 15,
'name' => __( 'Another fee', 'woocommerce' ),
),
);
// Loop through cart contents
foreach ( $cart->get_cart_contents() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Loop trough settings array
foreach ( $settings as $setting ) {
// Search for the product ID
if ( $setting['product_id'] == $product_id ) {
// Add fee
$cart->add_fee( $setting['name'], $setting['amount'], false );
}
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
Related: How to sum the additional fees of added product ID's in WooCommerce cart