Add SKU-based lookup for Inventory Synchronization#471
Add SKU-based lookup for Inventory Synchronization#471faisal-alvi wants to merge 4 commits intotrunkfrom
Conversation
🔍 WordPress Plugin Check Report
📊 Report
❌ Errors (8)📁 includes/Admin/Product_Editor_Compatibility.php (1 error)
📁 includes/WC_Payments_Compatibility.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-field-customer-id.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-order-partial-capture.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-admin-gateway-status.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor-token.php (1 error)
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-section.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
|
|
||
| if ( $child instanceof \WC_Product && $child->get_manage_stock() && $inventory_change ) { | ||
| $child_square_id = Product::get_square_item_variation_id( $child_id, false ); | ||
| if ( ! $child_square_id && $child->get_sku() && $sku_lookups_this_step < self::MAX_SKU_LOOKUPS_PER_PUSH_STEP ) { |
There was a problem hiding this comment.
What will happen when $sku_lookups_this_step becomes greater than self::MAX_SKU_LOOKUPS_PER_PUSH_STEP? Will it be skipped for the rest of the products?
There was a problem hiding this comment.
The cap is there to bound SearchCatalogObjects traffic per step to reduce rate-limit risk; so the limit applies per run of that step, not for the entire sync forever.
If the cap is reached within that run, we stop doing further SKU lookups for remaining unmapped lines in that same run. The push_inventory step keeps scheduling until inventory_push_product_ids is empty, so subsequent runs reset the counter and can perform more lookups. Successful lookups persist the variation ID, so those SKUs typically won’t need another lookup.
|
@iamdharmesh You’re right that we don’t call I agree that reproducing the original report purely by “slowing So the SKU lookup in |
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