Search code examples
phpmysqlwordpresshook-woocommerce

Missing argument within custom function in functions.php


This is related to a Wordpress website using Woocommerce. My server error logs log this error for each IP that visits a certain page:

Error: PHP Warning: Missing argument 2 for elixa_woo_hide_in_loop() in "filepath"/functions.php on line 239

Here's the code:

/*Within functions.php*/

add_filter( 'woocommerce_product_is_visible', 'elixa_woo_hide_in_loop', 2 );

function elixa_woo_hide_in_loop( $visible, $id ) {
echo "$id outside of if";
if ( $visible && get_field ( '_elixa_hide_cat', $id ) ) {
    echo "$id in if";
    return false;
    }
return $visible;
}

/*Within website, displayed above each product*/

Warning: Missing argument 2 for elixa_woo_hide_in_loop() in "filepath"/functions.php on line 239

outside of if

Here's the hook referenced above:

https://web.archive.org/web/20171025175244/http://woocommerce.wp-a2z.org/oik_hook/woocommerce_product_is_visible

The code above effects a Woocommerce "Browse all products" page where taxonomy-product_cat.php displays all products by category.

This code was custom written by a developer before me, and is not part of Wordpress or Woocommerce framework.

I cannot for the life of me understand what this is supposed to do for the loop. Strangely enough, when I remove the add_filter, it has zero effect on the product display and the errors disappear.

When tested $id = NULL or 0 to all these queries and I cannot find where this generic variable is being initially declared:

print gettype($id);
print get_object_vars($id);
print is_array($id);
print is_object($id);
print_r($id);
print count($id);
print get_class($id);
print isset($id);
print get_parent_class($id);
print gettype($id->container);

Just want to reach out to the community and see if they have seen something similar to this. Is this a sanitizing function? What does it do?


Solution

  • You have a mistake when registering the filter. The third parameter is priority, not number of arguments. See https://developer.wordpress.org/reference/functions/add_filter/

    Change your code to:

    add_filter( 'woocommerce_product_is_visible', 'elixa_woo_hide_in_loop', 10, 2 );