Fix ComparisonOperation Fatal Error in Woocommerce for PHP8+

- Woocommerce, WordPress

As of this writing there is currently a fatal error in the Woocommerce plugin for WordPress causing websites to crash. This specifically effects any websites running PHP8+. Below is the specific error generated in the debug.log file.

PHP Fatal error:  Uncaught TypeError: strpos(): Argument #1 ($haystack) must be of type string, array given in /wp-content/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php:49

A temporary fix can be applied by replacing the ComparisonOperation function in ComparisonOperation.php with the below code:

class ComparisonOperation {

public static function compare( $left_operand, $right_operand, $operation ) {
switch ( $operation ) {
case '=':
return $left_operand === $right_operand;
case '<': return $left_operand < $right_operand; case '<=': return $left_operand <= $right_operand; case '>':
return $left_operand > $right_operand;
case '>=':
return $left_operand >= $right_operand;
case '!=':
return $left_operand !== $right_operand;
case 'contains':
if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
return in_array( $right_operand, $left_operand, true );
}
if ( is_string( $right_operand ) && is_string( $left_operand ) ) {
return strpos( $right_operand, $left_operand ) !== false;
}
case '!contains':
if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
return ! in_array( $right_operand, $left_operand, true );
}
if ( is_string( $right_operand ) && is_string( $left_operand ) ) {
return strpos( $right_operand, $left_operand ) === false;
}
case 'in':
if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
return in_array( $left_operand, $right_operand, true );
}
if ( is_string( $left_operand ) && is_string( $right_operand ) ) {
return strpos( $left_operand, $right_operand ) !== false;
}
case '!in':
if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
return ! in_array( $left_operand, $right_operand, true );
}
if ( is_string( $left_operand ) && is_string( $right_operand ) ) {
return strpos( $left_operand, $right_operand ) === false;
}
}

return false;
}
}

This has been reported to the Woocommerce team and we expect them to release a patch soon via an update. For more help, you can email us at [email protected].