Fatal error when product does not have categories
-
My client does not use categories for his WooCommerce products. This has caused a fatal error in this plugin.
The error message is:
[20-Nov-2024 17:15:52 UTC] PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in /wp-content/plugins/official-facebook-pixel/integration/FacebookWordpressWooCommerce.php:126
Stack trace: 0 /wp-content/plugins/official-facebook-pixel/integration/FacebookWordpressWooCommerce.php(126): count(false) 1 /wp-content/plugins/official-facebook-pixel/integration/FacebookWordpressWooCommerce.php(116): FacebookPixelPlugin\Integration\FacebookWordpressWooCommerce::getProductCategory(12076)I have edited the getProductCategory() function in integration/FacebookWordpressWooCommerce.php:
private static function getProductCategory($product_id){
$categories = get_the_terms(
$product_id,
'product_cat'
);
// return count($categories) > 0 ? $categories[0]->name : null;
return ( is_array( $categories ) && count($categories) > 0) ? $categories[0]->name : null;
}get_the_terms() returns false when there are no categories.
BTW, the
is_array($categories)
check could be simplified to$categories
.
- You must be logged in to reply to this topic.