在WooCommerce PDF发票中显示优惠券折扣后的商品小计
Hey there! Let's figure out why swapping $totals for $discount isn't working to show the post-coupon item cost on your invoice. Here are some practical fixes to try:
First, confirm your
$discountvariable is calculated correctly
It’s possible the variable isn’t actually holding the discounted amount you expect. Double-check where you define it—make sure it’s properly subtracting the coupon value from the base price. For example:// Adjust these variables to match your code's structure $base_item_cost = 99.99; // Original pre-coupon price $coupon_amount = 15.00; // Amount the coupon covers $discount = $base_item_cost - $coupon_amount; // Should equal 84.99 hereAlso, make sure this variable is being passed into the context where your invoice template renders. If it’s not sent to the template at all, referencing
$discountwon’t display anything meaningful.Check for typos or scope issues in your template
Sometimes the problem is as simple as a misspelled variable name (like$discountsinstead of$discount) or the variable not being accessible in your template’s scope. Add a quick debug line to your invoice template to see what the variable actually contains:<!-- Temporarily add this to your invoice template --> <?php var_dump($discount); ?>If it outputs
NULLor the original base price, you know the variable isn’t being set correctly upstream.Look for code that overwrites the
$discountvariable
Scan through the code that runs right before rendering the invoice. Is there any line that resets$discountback to the base price? For example, a function that recalculates order totals without accounting for the coupon could be overwriting your value.Ensure coupon logic is applied to item-level costs
If your coupon only affects the overall order total but not individual item costs,$discountmight not reflect the per-item discounted price you need for the invoice. You may need to adjust your coupon logic to calculate and store the discounted cost for each item separately.
Once you’ve sorted out these checks, updating your invoice template to use the correctly populated $discount variable should display the post-coupon item cost as expected.
内容的提问来源于stack exchange,提问作者TomaszKub

