--- title: Stop WooCommerce coupons from selling below product cost description: Coupon stacking can push orders below cost on tight-margin products. A small plugin enforces a per-product cost floor at checkout. source: https://steem.dev/blog/woocommerce-block-coupons-below-cost published: 2026-09-16 updated: 2026-10-31 alternative_to: Advanced Coupons site: Steem — AI WordPress plugin generator --- # Stop WooCommerce coupons from selling below product cost WooCommerce coupons can be restricted by combination, excluded from products and capped. What they cannot do is look at a product's actual cost and refuse a combination that makes the sale unprofitable. A 30% sitewide coupon on top of a 20% sale, on a product with a 40% markup, leaves almost nothing, and WooCommerce has no idea. The usual workaround is an exclusion list on each coupon. It holds until the catalogue changes, the lists go stale, and someone stacks two discounts on a tight-margin product. Maintaining per-coupon exclusions is work that multiplies with every new code and every new product line. This plugin adds a cost field to each product. At checkout, if any item would sell below its cost once all coupons are applied, the coupons come off and the customer is told why. There are no lists to maintain; one rule covers every coupon. ## What one build covers - Product cost field in the pricing tab for simple and variable products - Per-variation cost entry for variable products - Checkout validation that removes all coupons if any item's effective unit price falls below its stored cost - Cart notice naming the specific product that triggered the removal ## What it does not cover - Shipping and gateway fees are not included in the margin calculation - Products without a cost entered are not protected until a cost is added - Immediate coupon validation on the cart page before the customer reaches checkout - Removing only the offending coupon while preserving others that would not cause a violation - Automatic sync from external cost sources such as supplier feeds or accounting software ## The prompt ```text Build a WordPress plugin called WP Margin Guard. Plugin Name: WP Margin Guard. Version: 1.0.0. Description: Block WooCommerce coupons that would discount any item below its entered product cost. Wrap all code in a class WP_Margin_Guard with a public init() method; instantiate the class and call init() on plugins_loaded. Product cost field: hook woocommerce_product_options_pricing. Call woocommerce_wp_text_input with array: id '_product_cost', label 'Product cost', description 'Coupons are removed at checkout if this item would sell below this price after discounts. Leave blank to skip validation for this product.', type 'number', custom_attributes array('min'=>'0','step'=>'0.01'). Hook woocommerce_process_product_meta with $post_id argument. Read $_POST['_product_cost'], sanitise with wc_format_decimal(). If the result is a non-empty string, update_post_meta($post_id,'_product_cost',$value); otherwise delete_post_meta($post_id,'_product_cost'). Variation cost field: hook woocommerce_variation_options_pricing with three arguments ($loop, $variation_data, $variation). After the variation sale price, output a paragraph with a label 'Variation cost' and a number input: name 'variable_cost_price[{$loop}]', value esc_attr(get_post_meta($variation->ID,'_product_cost',true)), class 'wc_input_price', min '0', step '0.01'. Hook woocommerce_save_product_variation with two arguments ($variation_id, $i). If isset($_POST['variable_cost_price'][$i]), sanitise with wc_format_decimal(), and if the result is a non-empty numeric string call update_post_meta($variation_id,'_product_cost',$value); else delete_post_meta($variation_id,'_product_cost'). Cart validation: hook woocommerce_check_cart_items. If WC()->cart->get_applied_coupons() is empty, return. Loop through WC()->cart->get_cart() as $cart_item_key => $item. For each item: $cost_id = !empty($item['variation_id']) ? $item['variation_id'] : $item['product_id']; $cost = get_post_meta($cost_id,'_product_cost',true). If empty($cost) or !is_numeric($cost), continue to next item. $effective_price = floatval($item['line_total']) / max(1, $item['quantity']). If $effective_price < floatval($cost): $name = esc_html($item['data']->get_name()); foreach WC()->cart->get_applied_coupons() as $code call WC()->cart->remove_coupon($code); wc_add_notice(sprintf('Your coupons have been removed. %s would sell below its cost price after the applied discounts. You can re-apply a coupon that keeps all items above cost.',$name),'notice'); return. No settings page, no stylesheet, no script. ``` ## What WooCommerce coupon controls already do Three native settings deal with stacking. 'Individual use only' stops any other coupon being used in the same order. 'Cannot be used with' names specific codes that cannot be combined with this one. 'Exclude products' and 'Exclude categories' keep a coupon off particular items. Used carefully, they cover a lot. The cost is maintenance: every coupon carries its own exclusions, and they have to change whenever the catalogue does. A store running welcome popups, affiliate codes and seasonal sales at once ends up with a matrix of rules nobody fully understands. Advanced Coupons and Flycart Discount Rules extend that model with condition groups, maximum discounts and stacking rules by coupon type. They fit when the problem is which coupons combine. They do not help when the problem is that the store does not know your cost. ## A cost field and a checkout check A 'Product cost' field sits in the pricing tab beside the regular and sale prices, and each variation of a variable product gets its own. Leave it blank and nothing changes for that product or variation; fill it in and that figure becomes the checkout floor, so you can roll costs out product by product. At checkout, the plugin divides each line's after-discount total by its quantity to get the effective unit price. If any item comes in below its cost, every applied coupon is removed, and a notice names the product that caused it. The codes stay visible on the cart page so the customer can see what was taken off. The check runs in WooCommerce's checkout verification step, so it catches coupons however they were added, typed in or applied from a URL. It does not run on the cart page: a customer can add a coupon without any error and only meets the check when they try to pay. ## Fees, blank costs and all-or-nothing removal Shipping and payment gateway fees are not counted; the comparison is item price against product cost. A sale at exactly cost still loses money after the processor and the courier take their share, so if those matter, enter a cost above your purchase price to build in a buffer. Products without a cost are not checked, which includes everything catalogued before the plugin went in until you fill the field. A violation removes all coupons, not just the culprit. A customer with a valid 5% loyalty code and a 60% flash sale code stacked loses both. A follow-up message can remove only the coupon responsible and keep the rest, and run the same check on the cart page so the customer hears about it straight away. ## Coupon rules plugins, and costs that live elsewhere If the real problem is which coupon types may combine, not a cost floor, Advanced Coupons or Flycart Discount Rules fit better. Rules like 'no two percentage coupons together' or 'no more than 20 euros off per order' need no cost data at all. If your costs already live in a supplier feed, a WMS or accounting software, typing them into the product admin will drift as costs change. Updating the field changes the floor from the next order, but the lasting fix is a small sync from that source into the `_product_cost` meta key rather than relying on someone remembering to edit it. --- Source: https://steem.dev/blog/woocommerce-block-coupons-below-cost Build this plugin: https://steem.dev/app