Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable
Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable
A PHP error that appears in many WordPress Plugins which have not been updated for a long time or are incompatible with newer versions of PHP. PHP Warning: Sizeof (): Parameter must be an array or an Object that implements countable.
content
In our scenario, the PHP error appeared at a module Cross Sell Product Display for WooCommerce.
FastCGI sent in stderr: "PHP message: PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable in /web/path/public_html/wp-content/plugins/cross-sell-product-display-for-woocommerce/templates.php on line 18
De ce apare eroarea PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable ?
The problem that generates this PHP error is the function sizeof() which in the version of PHP 7.2 or subsequent versions, can generate this error, if the given parameter is not a array or an object that implements the interface Countable.
Therefore, the error often appears after an update of the PHP version.
How do you solve PHP errors generated by sizeof()?
The simplest method is to replace the call to the function sizeof() with a call to the function count().
In the case of those who use the old versions of the module Cross Sell Product Display, the solution is simple. The functions from line 18 will be replaced templates.php.
function cdxfreewoocross_get_cross_sell_products($product_id=false){
if($product_id ===false ){
if(!is_product()){return false;}
$product_id = (int)get_the_ID();
if($product_id=='0'){return false;}
}
$crosssells = get_post_meta( $product_id, '_crosssell_ids',true);
if ( sizeof($crosssells ) == 0 || $crosssells =='') { return false; }
return $crosssells;
}
The above code in which SIZEOF () will be replaced with:
function cdxfreewoocross_get_cross_sell_products($product_id=false){
if($product_id ===false ){
if(!is_product()){return false;}
$product_id = (int)get_the_ID();
if($product_id=='0'){return false;}
}
$crosssells = get_post_meta( $product_id, '_crosssell_ids',true);
if ( !is_array( $crosssells ) || count( $crosssells ) == 0 || $crosssells =='') { return false; }
return $crosssells;
}
This change first checks if $crosssells It's a array using the function is_array() and otherwise returns false.
- [Fix PHP 7.2 / PHP 7.3] PHP Warning: Use of undefined constant (this will throw an Error in a future version of PHP)
- How to clean huge wp_options in SQL – Transients, wpseo_sitemap _cache_validator
- Fix wc-ajax = get_refreshed_fragments High CPU Usage (Disable AJAX Cart Fragments)
- How to customize the page "No Products Were Found Matching Your Selection" in WooCommerce - Optimizing sales on online stores
- How to clean huge wp_options in SQL – Transients, wpseo_sitemap _cache_validator
If $crosssells It's a array, the function is used count() to determine the number of items in array. If the number of items is zero or $crosssells It is a vacuum string, it returns false.
Leave comments if clarifications or completions are to be made to this tutorial.
Fix PHP Warning: sizeof(): Parameter must be an array or an object that implements Countable
What’s New
About Stealth
Passionate about technology, I write with pleasure on stealthsetts.com starting with 2006. I have a rich experience in operating systems: Macos, Windows and Linux, but also in programming languages and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, Presashop).
View all posts by StealthYou may also be interested in...