Last active
July 24, 2024 07:30
-
-
Save braddalton/5af238b6509715d025af3ae0c5c477c2 to your computer and use it in GitHub Desktop.
Add Fee for Specific Product ids in WooCommerce https://wpsites.net/wordpress-tutorials/3-ways-to-increase-fees-for-specific-products-in-woocommerce/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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