• 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.php file.

    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 getProductCategory function on line 126 of the FacebookWordpressWooCommerce.php file. The function attempts to use the count() function on a variable $categories which can be bool if get_the_terms returns false.

    Proposed Solution: To resolve this issue, the function should first check if $categories is an array or an instance of Countable before calling count(). 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 TypeError when $categories is false.

    The error occurs during the execution of the trackViewContentEvent function.

    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.