Skip to content

SQUARE-398: The order path calls Square's inventory history during checkout, and falls back to a drifting adjustment - #601

Draft
faisal-alvi wants to merge 11 commits into
trunkfrom
SQUARE-398
Draft

faisal-alvi wants to merge 11 commits into
trunkfrom
SQUARE-398

Conversation

@faisal-alvi

@faisal-alvi faisal-alvi commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

All Submissions:

  • Does your code follow the WooCommerce Sniffs variant of WordPress coding standards?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully run tests with your changes locally?
  • Will this change require new documentation or changes to existing documentation?

Changes proposed in this Pull Request:

For orders paid through a non-Square gateway (Cash on delivery, PayPal and so on), the order stock sync asked Square's inventory history on every order and every cancel restock, to decide between an absolute count (item never counted in Square) and a relative adjustment. That put a Square round trip inside every checkout. When the lookup failed, it silently sent an adjustment anyway, so an item Square had never counted went negative in Square (WooCommerce 4, Square -1).

This PR:

  • Remembers per product that Square holds a count, so only a product's first order asks Square's history and later checkouts skip that call. The flag is keyed by location and Square variation, so a relink or a location change resets it on its own.
  • Never guesses. When the history lookup fails, the stock change is held on the product and sent by a background Action Scheduler action, retried every 15 minutes until Square answers. Later orders for the same product queue behind a held change, so changes reach Square in order.
  • Drops a held change when Square received a newer absolute count in the meantime (for example from a product sync), since that count already includes the held sale and applying both would double count.
  • Keeps the new meta off duplicated products.

Unchanged on purpose, to keep the change small and free of regressions: the adjustment push itself still runs during checkout for known products, and a failed push is still logged and skipped as before; each product's first order after updating makes one history lookup; the refund path for other gateways is unchanged.

Closes https://linear.app/a8c/issue/SQUARE-398/the-order-path-calls-squares-inventory-history-during-checkout-and

Steps to test the changes in this Pull Request:

Setup: WooCommerce as system of record, inventory sync on, Square debug logging on, Cash on delivery enabled, connected to a Square sandbox. Square API calls show in WooCommerce > Status > Logs (square log); batchRetrieveInventoryChanges is the history lookup.

  1. Create a simple product synced with Square with Manage stock off, and let it sync. Then turn Manage stock on with quantity 5 and save. Confirm in the Square dashboard that the item has no count yet.
  2. Buy 1 of it on the storefront with Cash on delivery. In the log, the checkout request makes one batchRetrieveInventoryChanges call, then a PHYSICAL_COUNT of 4. Square shows 4.
  3. Buy 1 more. The log shows only an ADJUSTMENT of 1 for this checkout, with no batchRetrieveInventoryChanges. Square shows 3, WooCommerce shows 3.
  4. Create a second product the same way as step 1 (WooCommerce 5, no count in Square). Add this temporary mu-plugin to simulate Square failing the history lookup:
    <?php
    add_action( 'wc_square_api_request_performed', function ( $request ) {
        if ( false !== stripos( (string) $request['uri'], 'batchRetrieveInventoryChanges' ) ) {
            throw new Exception( 'Simulated Square outage' );
        }
    } );
  5. Buy 1 of the second product with Cash on delivery. The order completes normally. Square still has no count (it does not go to -1). The log says the stock change was deferred to a background retry. WooCommerce > Status > Scheduled Actions shows a pending wc_square_resolve_inventory_baseline action for the product.
  6. Leave the mu-plugin in place and let the action run (or click Run). It fails again and is rescheduled 15 minutes later; nothing is sent to Square.
  7. Remove the mu-plugin and run the action. The log shows a PHYSICAL_COUNT of 4 from the background action. Square shows 4, WooCommerce shows 4.
  8. Buy 1 more of the second product. Only an ADJUSTMENT of 1 is sent, with no history lookup. Square and WooCommerce both show 3.
  9. Regression: cancel one of the orders above. Square receives an ADJUSTMENT adding the stock back, with no history lookup.
  10. Regression: pay an order with the Square credit card gateway and confirm stock still syncs as before (this path is not touched).

Changelog entry

Fix - Order stock sync for other payment gateways no longer asks Square's inventory history on every checkout, and holds stock changes for a background retry instead of guessing when Square's inventory history cannot be read.

@faisal-alvi faisal-alvi self-assigned this Nov 17, 2026
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Test the build

WordPress Playground - runs in your browser

ℹ️ Neither can connect to Square: signing in to Square has to send you back to the site, and a site that runs in your browser tab has no public address to come back to. Without a connection the store cannot take payments, sync products or receive webhooks, so the previews show what a merchant sees before connecting. Do not enter Square access tokens or other credentials in a preview. The links stop working 14 days after the last build.


Build info:

  • Latest commit: f3f8785
  • Build time: 2026-11-17 13:55:36 UTC

Note: the build is updated when a new commit is pushed to this PR.

@github-actions

Copy link
Copy Markdown

✅ WordPress Plugin Check Report

✅ Status: Passed

📊 Report

All checks passed! No errors or warnings found.


🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant