Bug Report: TypeError in getProductCategory Function
-
The error occurs when a product does not have an assigned category.
I am writing to report a bug in the “Official Facebook Pixel” plugin, specifically within the
FacebookWordpressWooCommerce.phpfile.I am encountering a fatal error when attempting to use the plugin with WooCommerce. The error message is as follows:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in official-facebook-pixel/integration/FacebookWordpressWooCommerce.php:126
Source of the Issue: The error occurs in the
getProductCategoryfunction on line 126 of theFacebookWordpressWooCommerce.phpfile. The function attempts to use thecount()function on a variable$categorieswhich can beboolifget_the_termsreturnsfalse.Proposed Solution: To resolve this issue, the function should first check if
$categoriesis an array or an instance ofCountablebefore callingcount(). Here is the modified version of the function:private static function getProductCategory($product_id) { $categories = get_the_terms($product_id, 'product_cat'); if (is_array($categories) || $categories instanceof Countable) { return count($categories) > 0 ? $categories[0]->name : null; } else { return null; } }By implementing this check, the function will no longer throw a
TypeErrorwhen$categoriesisfalse.The error occurs during the execution of the
trackViewContentEventfunction.Or at least display a message indicating that products do not have assigned categories.
The topic ‘Bug Report: TypeError in getProductCategory Function’ is closed to new replies.