Skip to content

Conversation

@devbodaghe
Copy link
Contributor

Description

Summary

Fixes a critical bug where products are removed from Facebook Commerce Manager product sets when a WooCommerce category name or slug is changed.

Problem

When a user edits a WooCommerce product category name:

  1. The product set in Facebook CM is updated with the new category name ✅
  2. However, the products themselves still have the OLD category name in their product_type field ❌
  3. The product set filter (product_type i_contains "Category Name") no longer matches the products
  4. Result: Products disappear from the set, even though they're still in the WooCommerce category

This particularly affected simple products, while variable products sometimes remained due to other sync triggers.

Solution

Modified ProductSetSync::on_create_or_update_product_wc_category_callback() to automatically resync all products in a category when the category is edited. This ensures the product_type field on products gets updated with the new category name, allowing the product set filter to continue matching them correctly.

Added new method sync_products_in_category() that:

  • Gets all products in the category (all statuses)
  • For variable products, syncs variations (not the parent, following established patterns)
  • For simple products, syncs the product itself
  • Triggers Facebook sync to update the product_type field

Related Issue

Addresses bug report where products were removed from Facebook CM product sets after category rename.

Dependencies

None - uses existing sync infrastructure.

Type of change

  • Fix (non-breaking change which fixes an issue)

Checklist

  • I have commented my code, particularly in hard-to-understand areas, if any.
  • I have confirmed that my changes do not introduce any new PHPCS warnings or errors.
  • I have checked plugin debug logs that my changes do not introduce any new PHP warnings or FATAL errors.
  • I followed general Pull Request best practices. Meta employees to follow this wiki.
  • I have added tests (if necessary) and all the new and existing unit tests pass locally with my changes.
  • I have completed dogfooding and QA testing, or I have conducted thorough due diligence to ensure that it does not break existing functionality.
  • I have updated or requested update to plugin documentations (if necessary). Meta employees to follow this wiki.

Changelog entry

Fixed: Products no longer removed from Facebook Commerce Manager product sets when category name or slug is changed

Test Plan

Manual Testing Steps

Test Case 1: Category Name Change

  1. Create a WooCommerce product category "Electronics" with 2 simple products
  2. Verify all products appear in the corresponding product set in Facebook Commerce Manager
  3. Rename the category to "Electronic Devices"
  4. Expected Result:
    • Product set name updates to "Electronic Devices" in Facebook CM
    • All 2 products remain in the set
    • Product count unchanged

Test Case 2: Both Name and Slug Change

  1. Create a category "Accessories" with multiple products
  2. Verify all products sync to Facebook CM
  3. Edit both name to "Tech Accessories" and slug to "tech-accessories"
  4. Expected Result: All products remain in the set with no data loss

Test Case 3: Variable Products

  1. Create category "Clothing" with:
    • 2 simple products
    • 1 variable product with 3 variations (total 5 items)
  2. Verify all 5 items appear in Facebook CM product set
  3. Rename category to "Apparel"
  4. Expected Result: All 5 items (2 simple + 3 variations) still appear in the set

Automated Testing

vendor/bin/phpunit tests/Unit/ProductSets/ProductSetSyncTest.php

Updated tests verify that sync_products_in_category() is called when categories are created or updated.

Code Review Focus Areas

  • includes/ProductSets/ProductSetSync.php lines 72-74: Product resync trigger
  • includes/ProductSets/ProductSetSync.php lines 242-286: New sync method
  • Follows same pattern as Admin/Product_Categories.php lines 401-411

Screenshots

Before

Issue: When category "Test Category" is renamed to "New Category":

  • Product set filter updated: {"and":[{"product_type":{"i_contains":"New Category"}}]}
  • Products still have: product_type: "Test Category"
  • Result: Filter doesn't match → Products disappear from set 🔴

After

Fixed: When category is renamed:

  • Product set filter updated: {"and":[{"product_type":{"i_contains":"New Category"}}]}
  • Products automatically resynced with: product_type: "New Category"
  • Result: Filter matches → All products remain in set ✅

Technical Details

Files Modified:

  • includes/ProductSets/ProductSetSync.php
  • tests/Unit/ProductSets/ProductSetSyncTest.php

Pattern Consistency:
This implementation follows the exact same pattern used in Admin/Product_Categories::save_google_product_category_and_enhanced_attributes() which resyncs products when category attributes change. The approach is consistent with how variable products are handled throughout the codebase (sync variations, not parents).

Performance Impact:
Minimal - product syncing is already an asynchronous operation and this occurs only during the relatively rare event of category name changes.

@meta-cla meta-cla bot added the CLA Signed label Oct 21, 2025
@devbodaghe devbodaghe added the changelog: fix Took care of something that wasn't working. label Oct 21, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Oct 21, 2025

📦 Latest Plugin Build

Built at: 2025-10-28T10:55:53.056Z
Commit: 9968f5b
Size: 1.4M

Download: Click here to download the plugin

To download: Click the link above → Scroll to bottom → Download "facebook-for-woocommerce" artifact

@github-actions github-actions bot added the type: bug The issue is a confirmed bug. label Oct 21, 2025
@meta-codesync
Copy link

meta-codesync bot commented Oct 21, 2025

@devbodaghe has imported this pull request. If you are a Meta employee, you can view this in D85169628.

@devbodaghe devbodaghe force-pushed the fix/sync-products-on-category-name-change branch from c55ce70 to 087021e Compare October 28, 2025 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: fix Took care of something that wasn't working. CLA Signed type: bug The issue is a confirmed bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants