If you’re integrating WooCommerce with external systems like mobile apps or custom platforms using the REST API, you might run into a frustrating problem: applying a store credit coupon results in the order total dropping to zero, even if the credit doesn’t cover the full amount. This can lead to incorrect orders, lost revenue, and confused customers.
In this detailed guide, we’ll break down why this happens with the WooCommerce Store Credit plugin (now available through WooCommerce.com, formerly by Themesquad) and provide a step-by-step fix using a custom code snippet. Whether you’re a developer troubleshooting API integrations or a store owner managing WooCommerce REST API orders, this article will help you resolve the store credit discount issue efficiently.
We’ll cover the root cause, symptoms, and a reliable solution tested with recent versions, including WooCommerce Store Credit 5.1.2 and WooCommerce 10.x or higher. By the end, you’ll have the tools to ensure accurate discount calculations in your API-driven workflows.
Table of Contents
Understanding the Problem
When creating orders through the WooCommerce REST API and applying a store credit coupon, the system mishandles the discount application. Specifically, WooCommerce temporarily sets all line item totals to zero as part of its internal calculation process. This is normal behavior, but the Store Credit plugin fails to step in and recalculate the proper discounts afterward.
The coupon logic in WooCommerce operates in two main stages:
- Initial Reset: WooCommerce zeros out the totals to prepare for applying discounts based on coupon rules.
- Recalculation: The Store Credit plugin should then distribute the actual discount amounts across the line items, considering the available credit balance.
The issue arises because, in REST API requests, the plugin’s recalculation logic doesn’t trigger automatically. It relies on context cues (like whether the request is from the frontend checkout) that aren’t present in API calls. As a result, the discounts remain at zero, and the order finalizes with an incorrect total.
Symptoms of the Issue
Recognizing the problem early can save you time. Here are the common signs when applying a store credit coupon (e.g., something like “CREDIT-123”) during order creation via the REST API:
- The final order total becomes 0.00, regardless of the actual cart value.
- Discounts are not calculated or applied correctly to individual line items.
- The store credit balance on the coupon isn’t reduced or updated.
- The order appears as “free” in your WooCommerce dashboard, even though the credit only partially covers the cost.
These symptoms can disrupt inventory tracking, payment processing, and customer trust if not addressed.
Root Cause: Frontend vs. REST API Flows
To fix this, it’s essential to understand how WooCommerce and the Store Credit plugin interact in different scenarios.
Frontend Checkout Flow
In a standard browser-based checkout:
- The cart totals are loaded and initialized.
- WooCommerce applies coupons, temporarily setting line item totals to zero for recalculation.
- The Store Credit plugin detects the checkout context (using functions like wc_store_credit_is_request()) and recalculates the discounts based on the coupon’s balance, item prices, and taxes.
- Correct totals are restored, and the order proceeds normally.
This flow works seamlessly because the plugin has access to frontend-specific hooks and contexts.
REST API Flow
When creating orders via the REST API, there is no frontend cart context. The Store Credit plugin relies on functions like wc_store_credit_is_request() to detect
where the request originated from — e.g., checkout page, cart page, or REST API. But the plugin does not handle the REST API context correctly.
So WooCommerce reaches step 2 (sets totals to zero), but step 3 never happens — the Store Credit plugin doesn’t recalculate anything. The result? Every item discount remains 0, and WooCommerce finalizes the order with a total of $0.00.
The Fix: Implementing a Custom Hook
Fortunately, you can resolve this with a simple, targeted code snippet that manually triggers the recalculation during REST API requests. This fix is compatible with WooCommerce Store Credit version 5.1.2 (or similar recent releases) and WooCommerce 10.x or higher.
Environment Requirements
- Plugin: WooCommerce Store Credit (available via WooCommerce.com)
- Version: 5.1.2 or compatible
- WooCommerce Version: 10.x or higher
- Scenario: Creating orders via WooCommerce REST API with store credit coupons applied
- Symptom Addressed: Order total incorrectly set to 0.00, with store credit not deducted properly
The Code Snippet
Add the following PHP code to your site. It uses a WooCommerce filter to intervene in the discount calculation process.
Fix: Store Credit coupon discount not calculated in WooCommerce REST API orders
@author Md Maraj Rashid
@description
When a Store Credit coupon is applied via REST API, WooCommerce resets item totals to 0
for recalculation, but the Store Credit plugin never re-applies the correct discounts.
This filter detects REST API requests and triggers the recalculation manually.
add_filter( 'woocommerce_coupon_custom_discounts_array', 'fix_store_credit_rest_api_discounts_array', 20, 2 ); function fix_store_credit_rest_api_discounts_array( $discounts, $coupon ) { // Only run for Store Credit coupons if ( ! function_exists( 'wc_is_store_credit_coupon' ) || ! wc_is_store_credit_coupon( $coupon ) ) { return $discounts; } // Ensure we're dealing with a REST API request if ( ! function_exists( 'wc_store_credit_is_request' ) || ! wc_store_credit_is_request( 'rest_api' ) ) { return $discounts; } // Try to retrieve the order from the first order item ID $order_item_ids = array_keys( $discounts ); $order_item = new WC_Order_Item_Product( $order_item_ids[0] ); $order = $order_item->get_order(); if ( ! $order ) { return $discounts; } // Recalculate the discount properly using the Store Credit class $order_discounts = new WC_Store_Credit_Discounts_Order( $order ); $new_discounts = $order_discounts->calculate_item_discounts( $coupon, wc_remove_number_precision_deep( $discounts ) ); return wc_add_number_precision_deep( $new_discounts ); }
Where to Place This Code
Depending on your setup, choose one of these options to implement the snippet safely:
- In Your Child Theme’s functions.php: Ideal if your REST API integration ties into frontend elements (e.g., a custom site or mobile app). Add it to wp-content/themes/your-child-theme/functions.php. This keeps it theme-specific and easy to manage.
- In a Custom Plugin: Recommended for headless WooCommerce setups or complex integrations. Create a simple plugin file (e.g., fix-store-credit-rest-api.php) in wp-content/plugins/, add the code, and activate it. This ensures the fix persists through theme updates.
- Via a Code Snippet Plugin: Use tools like WPCode or similar to insert the snippet without editing files directly. This is great for non-developers.
Always back up your site before adding code, and test in a staging environment.
What This Code Does
Let’s break it down step by step:
- Hooks into WooCommerce: It uses the woocommerce_coupon_custom_discounts_array filter to modify discount calculations.
- Checks Conditions: Verifies that the coupon is a store credit type and the request is from the REST API.
- Retrieves Order Data: Pulls the current order from the first line item to access necessary details.
- Triggers Recalculation: Instantiates the plugin’s WC_Store_Credit_Discounts_Order class to compute accurate discounts based on the coupon balance, item totals, and taxes.
- Applies Corrections: Returns the updated discount array, ensuring the order total reflects the real amount after credit deduction.
This approach mimics the frontend behavior without altering core plugin files.
Example in Action
Consider a practical scenario:
- Order subtotal: $150.00
- Store credit coupon balance: $50.00
Before the Fix:
- Via REST API, WooCommerce resets totals but skips recalculation.
- Result: Order total = $0.00 (incorrectly free order).
After the Fix:
- The snippet triggers proper discount application.
- Result: Discount = $50.00, Order total = $100.00 (credit deducted correctly, balance updated).
This ensures accurate billing and prevents revenue loss.
Internal Plugin Insight for Advanced Developers
The WooCommerce Store Credit plugin’s core logic revolves around the WC_Store_Credit_Discounts_Order class, which handles:
- Evaluating the coupon’s remaining balance.
- Distributing discounts proportionally across line items.
- Accounting for tax configurations and other variables.
Normally, this class activates during frontend or AJAX-driven checkouts. By manually creating an instance in our filter, we bridge the gap for REST API scenarios without invasive changes.
Results and Testing
After applying the fix, test these scenarios to confirm everything works:
Scenario | Expected Outcome |
---|---|
Order via REST API with store credit coupon | Total reduced by exact credit amount; balance updated. |
Store credit balance tracking | Credit deducted correctly from coupon. |
Multiple coupons (store credit + promo code) | Both discounts applied without conflicts. |
Order creation in wp-admin | Unaffected; works as before. |
Regular frontend checkout | Unaffected; no changes to standard flow. |
The fix is isolated, so it won’t impact non-API or non-store-credit orders.
Developer Notes
- Safety First: This snippet only activates for store credit coupons in REST API contexts, making it non-invasive.
- Compatibility: Tested with WooCommerce 10.x+ and Store Credit 5.1.2; the hook should remain stable through updates.
- No Side Effects: Frontend checkouts and other coupon types are untouched.
- Troubleshooting Tip: If issues persist, check your API request payload for correct coupon application (e.g., via the /wp-json/wc/v3/orders endpoint).
Summary
Topic | Details |
---|---|
Issue | Store credit coupon sets order total to 0.00 via REST API. |
Cause | Plugin’s discount logic doesn’t trigger for API requests. |
Fix | Custom filter to manually recalculate discounts. |
Plugin | WooCommerce Store Credit (via WooCommerce.com). |
Recommended Placement | Custom plugin or child theme’s functions.php. |
WooCommerce Tested | 10.x+. |
TL;DR
The WooCommerce Store Credit plugin doesn’t automatically handle discount calculations for REST API orders, as it expects a frontend context. Adding a custom filter to trigger recalculation ensures correct totals and credit deductions in API-created orders.
Conclusion
Fixing the Woo Commerce Store Credit discount issue via the REST API is a straightforward process once you understand the root cause: the plugin’s failure to recalculate discounts in API-driven orders due to missing frontend context. By implementing the provided custom code snippet, you can ensure accurate discount application, proper store credit balance tracking, and correct order totals, all while maintaining compatibility with WooCommerce 10.x and Store Credit 5.1.2 or later. This solution is safe, non-invasive, and preserves the integrity of frontend checkouts and other coupon types. Whether you’re a developer building a headless WooCommerce store or a store owner managing API integrations, this fix empowers you to deliver a seamless shopping experience without revenue loss or customer confusion. Add the snippet to your child theme, custom plugin, or code snippet tool, test thoroughly, and enjoy reliable store credit functionality in your REST API workflows.