WooCommerce单个产品添加百分比附加费解决方案咨询
Hey there, totally get where you’re coming from—custom cabinet door shops rely on flexible pricing structures, and missing percentage-based add-ons can be a real pain when official plugins don’t cover it. Luckily, there are a few solid workarounds you can implement right now:
1. Custom Code Snippet (Free & Flexible)
If you’re comfortable with a bit of code, this is the most customizable option. You can use WooCommerce’s built-in hooks to calculate and apply percentage fees based on specific product attributes or user-selected add-ons.
Here’s an example snippet that adds a percentage fee when a specific add-on checkbox is selected:
// Save selected add-on to cart item data add_filter('woocommerce_add_cart_item_data', 'save_percentage_addon_selection', 10, 2); function save_percentage_addon_selection($cart_item_data, $product_id) { // Replace 'premium_finish' with your add-on field name if (isset($_POST['premium_finish']) && $_POST['premium_finish'] === 'yes') { $cart_item_data['apply_percentage_fee'] = true; $cart_item_data['percentage_rate'] = 0.15; // 15% fee // Unique key to prevent merging identical cart items $cart_item_data['unique_key'] = md5(microtime().rand()); } return $cart_item_data; } // Retrieve saved data from user session add_filter('woocommerce_get_cart_item_from_session', 'retrieve_percentage_addon_data', 10, 2); function retrieve_percentage_addon_data($cart_item, $values) { if (isset($values['apply_percentage_fee'])) { $cart_item['apply_percentage_fee'] = $values['apply_percentage_fee']; $cart_item['percentage_rate'] = $values['percentage_rate']; } return $cart_item; } // Calculate and apply the percentage fee add_action('woocommerce_cart_calculate_fees', 'apply_percentage_addon_fee'); function apply_percentage_addon_fee() { if (is_admin() && !defined('DOING_AJAX')) return; $fee_total = 0; foreach (WC()->cart->get_cart() as $cart_item) { if (isset($cart_item['apply_percentage_fee']) && $cart_item['apply_percentage_fee']) { // Calculate fee based on the item's subtotal $item_subtotal = $cart_item['line_subtotal']; $fee_total += $item_subtotal * $cart_item['percentage_rate']; } } if ($fee_total > 0) { WC()->cart->add_fee('Premium Finish (15%)', $fee_total); } }
- Replace
premium_finishwith your actual add-on field name (check your form’s HTML to find this) - Adjust the
percentage_rateto your desired percentage (0.15 = 15%) - Add this code to your child theme’s
functions.phpfile or a custom plugin (always back up your site before adding custom code!)
2. Upgrade to a More Flexible Add-On Plugin
If you’d rather avoid code, there are third-party plugins that natively support percentage-based add-ons:
- Product Add-Ons Ultimate: This plugin lets you create add-ons with both fixed and percentage-based pricing, plus it integrates smoothly with WooCommerce Measurement Price Calculator.
- WooCommerce Extra Product Options: Another robust option that supports percentage fees, conditional logic, and custom form fields tailored to custom products like cabinet doors.
These are paid plugins, but they’ll save you development time and come with support if you run into issues.
3. Extend Existing Plugins (For Advanced Users)
If you want to stick with your current plugins, you can extend their functionality with custom hooks (avoid editing core plugin files directly—updates will overwrite your changes!). For example, you can hook into the Product Add-Ons plugin’s fee calculation filter to add percentage-based logic:
add_filter('woocommerce_product_addons_calculate_fee', 'add_percentage_fee_to_addons', 10, 3); function add_percentage_fee_to_addons($fee, $addon, $value) { // Check if this add-on should be a percentage fee if ($addon['name'] === 'Premium Finish' && $value === 'Yes') { global $woocommerce; $cart_items = $woocommerce->cart->get_cart(); foreach ($cart_items as $cart_item_key => $cart_item) { $item_subtotal = $cart_item['line_subtotal']; $fee = $item_subtotal * 0.15; // 15% of product price } } return $fee; }
Note: You’ll need to adjust this snippet to match your add-on’s name and how the Measurement Price Calculator stores the final product price.
内容的提问来源于stack exchange,提问作者Luke Miller

