// Add a fixed custom fee to a specific WooCommerce product.
// This example adds a $5.00 fee to the product with ID 1234.
function add_custom_fee_to_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Check if product is in the cart
if ( is_object( WC()->cart ) ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == 1234 ) {
WC()->cart->add_fee( 'Custom Fee', 5.00 );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_custom_fee_to_product', 10, 1 );
Add custom fee to WooCommerce product
In