skip to Main Content

I’m having an issue using a hook from woocommerce. When looking into woocommerce’s code, I can clearly see the hook has two arguments:

do_action( 'woocommerce_new_product_variation', $id, $product );

But when I do this in my code:

add_action( 'woocommerce_new_product_variation', 'do_something', 10, 2 );

function do_something( $variation_id, $product ){
  echo "hello";
}

I get this with this error when creating a product variation:

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function do_something(), 1 passed in /var/www/html/website/wp-includes/class-wp-hook.php on line 287 and exactly 2 expected in /var/www/html/website/wp-content/themes/my-theme/functions/woocommerce/woocommerce-extras.php:5

I don’t follow as I’m giving add_action exactly 2 arguments to work with. But PHP is complaining that I’m passing 1 argument.

2

Answers


  1. Chosen as BEST ANSWER

    A plugin I was using was extending the WC_Product_Data_Store_CPT class, and replaced the hook with a single argument.


  2. I looks you dont correctly pass the two variables in your do_action statement. $id and $product need to exist.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search