Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active July 24, 2024 07:30
Show Gist options
  • Save braddalton/5af238b6509715d025af3ae0c5c477c2 to your computer and use it in GitHub Desktop.
Save braddalton/5af238b6509715d025af3ae0c5c477c2 to your computer and use it in GitHub Desktop.
// Add a flat rate fee for a specific product
add_action( 'woocommerce_cart_calculate_fees', 'add_flat_rate_fee_for_specific_product_v1', 10, 1 );
function add_flat_rate_fee_for_specific_product_v1( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$product_id = 123; // Replace with the ID of your specific product
$flat_rate_fee = 10; // Replace with your desired flat rate fee
foreach ( $cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
$cart->add_fee( 'Special Product Fee', $flat_rate_fee, true );
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment