【WooCommerce+Product Add-ons插件】选中特定产品附加选项时隐藏货到付款(COD)支付网关
Hey there! I totally get your scenario—you're using the WooCommerce Product Add-Ons plugin to add custom options like Stitching, Matching addons, and matching borders to all products in a category, and you need to hide the Cash on Delivery (COD) payment gateway when both Matching addons and Stitching are selected in the cart or checkout page. Since these add-ons show up as <dt> elements instead of having unique variant IDs, the standard variant-based solution won't work here. Let's fix this with a custom approach tailored to how Product Add-Ons stores its data.
Step-by-Step Solution
The core idea is to check every item in the cart for the presence of both target add-ons, then use a WooCommerce filter to remove the COD gateway if both are found.
1. Add Custom Code to Your Site
You can add this code to your child theme's functions.php file, or use a code snippet plugin (like Code Snippets) to keep it safe from theme updates:
// Helper function to check if cart has both Matching addons and Stitching add-ons function has_matching_addons_and_stitching() { // Exit early if cart is empty or unavailable if ( ! WC()->cart || WC()->cart->is_empty() ) { return false; } $has_matching = false; $has_stitching = false; // Loop through each item in the cart foreach ( WC()->cart->get_cart() as $cart_item ) { // Check if the item has Product Add-Ons data if ( isset( $cart_item['addons'] ) && is_array( $cart_item['addons'] ) ) { // Check each add-on for our target options foreach ( $cart_item['addons'] as $addon ) { // Match add-on names (adjust strings to exactly match your labels) if ( strtolower( trim( $addon['name'] ) ) === 'matching addons' ) { $has_matching = true; } elseif ( strtolower( trim( $addon['name'] ) ) === 'stitching' ) { $has_stitching = true; } // Early exit if both are found to save resources if ( $has_matching && $has_stitching ) { return true; } } } } return $has_matching && $has_stitching; } // Filter payment gateways to hide COD when both add-ons are present function hide_cod_for_selected_addons( $available_gateways ) { // Only run on cart and checkout pages if ( is_cart() || is_checkout() ) { // Remove COD if both add-ons are in the cart if ( has_matching_addons_and_stitching() && isset( $available_gateways['cod'] ) ) { unset( $available_gateways['cod'] ); } } return $available_gateways; } add_filter( 'woocommerce_available_payment_gateways', 'hide_cod_for_selected_addons' );
2. Adjust the Code to Match Your Add-On Labels
Make sure the strings inside strtolower( trim( $addon['name'] ) ) exactly match the names of your add-ons (case doesn't matter because we're converting to lowercase). For example, if your add-on is named "Matching Add-Ons" instead of "Matching addons", update that string accordingly.
3. Test the Functionality
- Add a product with both Matching addons and Stitching selected to your cart.
- Navigate to the cart or checkout page—COD should be hidden.
- Test with only one of the add-ons selected: COD should remain visible.
Troubleshooting Tip
If the code doesn't work right away, you can debug the add-on data structure by adding var_dump( $cart_item['addons'] ); inside the cart item loop. This will show you exactly how the Product Add-Ons plugin stores the option names and values, so you can adjust the matching logic if needed.
内容的提问来源于stack exchange,提问作者Amsal Khan

