Skip to content

Conversation

@sol-loup
Copy link
Contributor

Summary

Fixes #2894

Prevents fatal TypeError when wc_get_product() returns false by adding defensive type checking before rendering product fields in the admin.

Problem

The add_product_settings_tab_content() function calls wc_get_product($post) which can return false in certain scenarios:

  • Plugin conflicts (e.g., WooCommerce Dynamic Pricing)
  • Invalid post types
  • Missing product data

This false value is then passed to render_google_product_category_fields_and_enhanced_attributes() which expects a WC_Product type, causing a fatal TypeError that completely blocks product editing.

Error:

PHP Fatal error: Uncaught TypeError: WooCommerce\Facebook\Admin\Products::render_google_product_category_fields_and_enhanced_attributes(): 
Argument #1 ($product) must be of type WC_Product, bool given
in /wp-content/plugins/facebook-for-woocommerce/includes/Admin/Products.php on line 43

Solution

Added defensive validation in includes/Admin.php before calling the render function:

  1. Fallback retrieval: If $product is false/null, attempts to retrieve it using wc_get_product($post->ID)
  2. Type guard: Only calls render function if $product instanceof \WC_Product
  3. Error logging: Logs when product object is invalid for debugging
  4. Graceful degradation: Page continues to load even if fields can't be rendered

Changes

File: includes/Admin.php (lines 1318-1332)

  • Added product validation check before rendering Google product category fields
  • Included fallback product retrieval attempt
  • Added error logging for debugging invalid scenarios

Testing

  • Tested normal product editing with valid products
  • Verified no fatal errors occur with plugin conflicts
  • Confirmed PHP syntax is valid
  • Google product category fields render properly for valid products
  • Page loads gracefully when product object is invalid

Test Plan

  1. Edit various product types (simple, variable, grouped)
  2. Verify Facebook tab loads without errors
  3. Install WooCommerce Dynamic Pricing plugin (or similar)
  4. Edit products with the conflicting plugin active
  5. Confirm no fatal errors occur
  6. Verify Google product category fields render for valid products
  7. Check error logs for debugging messages (if product is invalid)

Risk Assessment

Risk Level: Very Low

  • Only adds defensive type guards - no logic changes
  • Existing functionality fully preserved for valid products
  • Prevents fatal errors while maintaining feature availability
  • Includes fallback mechanism for edge cases

Closes #2894

Fixes #2894

Prevent TypeError when wc_get_product() returns false by adding
defensive type checking before calling render_google_product_category_fields_and_enhanced_attributes().

- Add fallback to retrieve product using $post->ID if initial call fails
- Only render Google product category fields when valid WC_Product exists
- Add error logging for debugging invalid product scenarios
- Gracefully handle plugin conflicts (e.g., WooCommerce Dynamic Pricing)

This prevents fatal errors while maintaining full functionality for
valid products.
@github-actions github-actions bot added changelog: fix Took care of something that wasn't working. type: bug The issue is a confirmed bug. labels Oct 27, 2025
@meta-cla meta-cla bot added the CLA Signed label Oct 27, 2025
@github-actions
Copy link
Contributor

📦 Latest Plugin Build

Built at: 2025-10-27T19:10:16.332Z
Commit: ebd3904
Size: 1.4M

Download: Click here to download the plugin

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

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.

Plugin Conflict - causes lack of editing for WooCommerce Product

2 participants