Add SKU-based lookup for Inventory Synchronization#471
Conversation
🔍 WordPress Plugin Check Report
📊 Report
❌ Errors (8)📁 includes/WC_Payments_Compatibility.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor-token.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-order-partial-capture.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-field-customer-id.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-section.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-admin-gateway-status.php (1 error)
📁 includes/Admin/Product_Editor_Compatibility.php (1 error)
🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check |
iamdharmesh
left a comment
There was a problem hiding this comment.
Thanks for the PR @faisal-alvi. Have some questions could you please help answer it?
If an earlier
upsert_new_productsstep timed out (or failed) after Square created the item but before the plugin saved the mapping, those products stayed at 0 quantity in Square and never received a stock push.
If upsert_new_products step timed out, it will be retried and product upsertion will be retried. So, I don't anticipate the background process will reach to the push_invetory step without adding a reference to the meta. @faisal-alvi have you tried reproducing the issue by adding an intentional wait in the upsert_new_products function and making it timed out? Is it reaching to the push_invetory step? Thanks
|
@iamdharmesh You’re right that we don’t call I agree that reproducing the original report purely by “slowing So the SKU lookup in |
|
@iamdharmesh 👋🏻 I dug into this in depth in the codebase. You raised a fair point: if the sync dies mid- I still think we should merge this PR as defense in depth. The This PR is still justified as belt-and-suspenders at It's worth noting that PR #483 is addressing the timeout-related issues in |
iamdharmesh
left a comment
There was a problem hiding this comment.
Thanks for changes @faisal-alvi. I haven't tested this but based on the code this looks good overall now. I have added minor note to check.
I still think we should merge this PR as defense in depth. The
inventory_push_product_idsis filled from$staged_product_ids, not “mapping definitely succeeded for every line”.
Agree, However, could you please check if $staged_product_ids are not in the $successful_product_ids then is it even uploaded to Square? Because we are adding alert here in that case. Thanks.
Also, update SKU lookup logic to simplify incrementing lookup count
@faisal-alvi did you got a chance to look at this? |
|
Hi @iamdharmesh yes, I looked into this. The The So So for |
There was a problem hiding this comment.
Thanks for the clarification @faisal-alvi. Code looks good to me. However, I haven't tested this. @qasumitbagthariya / @ankitguptaindia Could you please test this thoroughly. Thanks
qasumitbagthariya
left a comment
There was a problem hiding this comment.
QA Update ✅
I have verified this PR in the SQUARE-264 branch, which has been fixed and is functioning as intended.
I tested the following on this branch:
- Simple product sync works correctly after removing _square_item_variation_id
- Variable product sync works; missing variation mapping restored via SKU lookup
- Inventory updates pushed successfully to Square
Testing Environment
Details
- WordPress: 6.9.4
- Theme: Storefront 4.6.2
- Theme: Twenty Twenty-Five 1.4
- WooCommerce - 10.7.0
- PHP: 8.0.30
- Web Server: Nginx 1.20.2
- Browser: Chrome
- OS: macOS 15.2
- Branch: SQUARE-264
Steps to Test- As mentioned in the PR description.
Test Results - It is working as expected.
Functional Demo / Screencast -
Special Notes - Ready for UAT
Testing Document status:
Cases related to this Issue/PR are added to the Critical Flow Wiki pages:
- Yes
- Not Required/Applicable for this PR
Regression Test Report- QA Verified ✅Testing Environment -Details
Result:Verified all key flows, user flows related to this PR, no blocking or functional issues detected. Final Status:Marked as Ready for Merge 🚀 |
All Submissions:
Changes proposed in this Pull Request:
When
push_inventoryruns, products/variations without a stored_square_item_variation_idmapping were previously skipped. If an earlierupsert_new_productsstep timed out (or failed) after Square created the item but before the plugin saved the mapping, those products stayed at 0 quantity in Square and never received a stock push.This PR adds SKU-based fallback so
push_inventorycan recover in that case:Product::get_square_variation_id_by_sku()Looks up a Square catalog variation by SKU via
SearchCatalogObjects(exact query onsku). Optionally persists the mapping so later sync steps use the stored ID. Exceptions are caught and logged; the method returnsnullso sync continues.push_inventory(Manual_Synchronization)MAX_SKU_LOOKUPS_PER_PUSH_STEP = 20limits how many SKU lookups run perpush_inventorystep to avoid rate limits; additional unmapped products are handled in later step runs.Note
The timeout that prevents mapping from being saved is intermittent (e.g. slow API, server limits), so this PR does not address that root cause. Instead, it targets the incomplete-sync case: when products already exist in Square but WooCommerce never received or stored the variation IDs (e.g. after a timeout). For those unmapped products, the plugin now attempts a SKU-based lookup during push_inventory—matching the approach suggested in the original issue—and persists the mapping when found so inventory can be pushed. This makes inventory push resilient to missed meta and looks promising for the reported scenario.
Closes https://linear.app/a8c/issue/SQUARE-264/push-inventory-should-attempt-sku-based-matching-for-unmapped-products.
Steps to test the changes in this Pull Request
_square_item_variation_idmeta (simulate lost mapping)._square_item_variation_id.Changelog entry