Skip to content

Fix Categories were being stripped from products during manual sync.#451

Draft
iamdharmesh wants to merge 13 commits intotrunkfrom
fix/SQUARE-231
Draft

Fix Categories were being stripped from products during manual sync.#451
iamdharmesh wants to merge 13 commits intotrunkfrom
fix/SQUARE-231

Conversation

@iamdharmesh
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:

In PR #408, we added support for syncing multiple categories. However, we missed adding this support to manual synchronization. This PR fixes an issue in manual and interval synchronization where categories were being stripped from products synced from Square.

Note

While working on this, I noticed that synced categories do not maintain the parent–child relationship during synchronization. I have reported a separate issue to explore implementation options and potentially address it.

Closes https://linear.app/a8c/issue/SQUARE-231/categories-get-stripped-off-from-products-that-are-synced-from-square

Steps to test the changes in this Pull Request:

  1. Square SOR:

    • Create a product in Square with multiple categories.
    • Sync from Square to WooCommerce (via import).
    • Verify that the WooCommerce product has all categories assigned correctly.
    • Update the product on the Square side (e.g., update the product title).
    • Wait for the interval polling to trigger (or trigger it manually).
    • Verify that the WooCommerce product still has all categories assigned correctly and that none are removed.
    • Trigger a manual sync from the settings and verify that the WooCommerce product still has all categories assigned correctly and that none are removed.
  2. WooCommerce SOR:

    • Create a product in WooCommerce with multiple categories and sync it with Square.
    • Verify that the Square product has all categories assigned correctly.
    • Update the product on the WooCommerce side (e.g., update the product title).
    • Verify that the Square product still has all categories assigned correctly and that none are removed.
    • Trigger a manual sync from the settings and verify that the Square product still has all categories assigned correctly and that none are removed.

Changelog entry

Fix - Categories were being stripped from products during manual sync.

@iamdharmesh iamdharmesh self-assigned this Jan 21, 2026
@vikrampm1 vikrampm1 added this to the Future Release milestone Feb 15, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 13, 2026

🔍 WordPress Plugin Check Report

❌ Status: Failed

📊 Report

🎯 Total Issues ❌ Errors ⚠️ Warnings
8 8 0

❌ Errors (8)

📁 includes/WC_Payments_Compatibility.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor-token.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-user-payment-token-editor.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-order-partial-capture.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-field-customer-id.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-user-profile-section.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Framework/PaymentGateway/Admin/views/html-admin-gateway-status.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;
📁 includes/Admin/Product_Editor_Compatibility.php (1 error)
📍 Line 🔖 Check 💬 Message
0 missing_direct_file_access_protection PHP file should prevent direct access. Add a check like: if ( ! defined( 'ABSPATH' ) ) exit;

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes category assignment loss during Square→Woo sync (manual + interval) by centralizing multi-category extraction/import and updating sync handlers and E2E coverage accordingly.

Changes:

  • Refactors Square category ID extraction/import into Category::get_category_ids_from_catalog_item() and reuses it in product import and product updates.
  • Updates product update logic to assign multiple WooCommerce categories instead of a single category.
  • Expands E2E utilities/specs to create, sync, and assert multiple categories.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/e2e/utils/square-sandbox.js Adds support for multiple categories in Square sandbox helpers and catalog info extraction.
tests/e2e/utils/helper.js Updates Woo product creation helper to handle multiple categories from a comma-delimited string.
tests/e2e/specs/d8.square-sor-multiple-variations.spec.js Updates expected variation attribute ordering/values (test expectations).
tests/e2e/specs/c4.product-import.spec.js Extends import test to validate multiple categories after Square import.
tests/e2e/specs/c2.woo-sor.spec.js Extends Woo→Square sync test to validate multiple categories pushed to Square.
includes/Sync/Product_Import.php Replaces inline category import logic with shared category-ID extraction helper.
includes/Handlers/Product.php Updates product-from-Square handler to set multiple category terms.
includes/Handlers/Category.php Introduces shared helper to map/import Square categories to Woo term IDs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread includes/Handlers/Category.php
Comment thread includes/Handlers/Category.php
Comment thread tests/e2e/utils/square-sandbox.js Outdated
Comment thread tests/e2e/utils/helper.js Outdated
Comment thread tests/e2e/specs/c4.product-import.spec.js
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@iamdharmesh iamdharmesh requested a review from faisal-alvi March 16, 2026 05:37
Comment thread tests/e2e/specs/c4.product-import.spec.js Outdated
Copy link
Copy Markdown
Collaborator

@faisal-alvi faisal-alvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamdharmesh thanks for working on this!

When Square has no categories, we should explicitly set product categories to empty so Woo matches Square, or document that we intentionally leave existing categories. I have also added a few suggestions. Please have a look.

Comment thread includes/Handlers/Category.php
Comment thread includes/Handlers/Category.php
try {
$response = wc_square()->get_api()->batch_retrieve_catalog_objects( $missing_category_ids );
if ( $response->get_data() instanceof \Square\Models\BatchRetrieveCatalogObjectsResponse ) {
$missing_categories = $response->get_data()->getObjects();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if getObjects() returns an empty array (e.g. deleted/invalid IDs), we only log on exception. Should we consider logging when some IDs were requested but none returned, for “category still missing” cases?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We log exception if we didn't get a proper response, but not logging for the empty array? Could you please provide more information on what we want to do here? Thanks

Comment thread includes/Handlers/Product.php Outdated
Comment thread tests/e2e/specs/c2.woo-sor.spec.js
Comment thread tests/e2e/specs/d8.square-sor-multiple-variations.spec.js
@iamdharmesh
Copy link
Copy Markdown
Collaborator Author

@faisal-alvi this is ready for your review.

@iamdharmesh iamdharmesh requested a review from faisal-alvi April 7, 2026 13:41
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.

4 participants