0

I am trying to get these parameters from the do_action placed inside body:

do_action( 'custom_action',  array( 'product_id' => $product_id ,  'outbiddeduser_id' => $outbiddeduser, 'log_id' => $log_id ) );

I am trying to do it like this:

    add_action('custom_action', 'test', 10, 3);
    function test($product_id, $outbiddeduser_id, $log_id) {
         $a = $product_id;
         $b = $outbiddeduser_id;
         $c = $log_id; 

     echo $a . ', ' . $b . ', ' . $c;
    }

And this:

add_action('custom_action', 'test', 10, 1);
function test( $associative_array ) {
   $a = $associative_array['product_id'];
   $b = $associative_array['outbiddeduser_id'];
   $c = $associative_array['log_id'];

   echo $a . ', ' . $b . ', ' . $c;
}

And it's not working. What am I doing wrong?

  • How are you testing this? The code examples as is don't have any means of checking if it worked or not – Tom J Nowell Sep 3 '19 at 17:40
  • Updated it again. – Tahi Reu Sep 3 '19 at 19:31
  • I see, if you don't get what you expected, what do you get instead? And can you update your code so it has values? There are variables, but it's unclear what their values are, which means it might work but it's getting empty values – Tom J Nowell Sep 4 '19 at 13:54
0

You passed the action an associative array, so your hooked function will recieve an associative array. It's a little clearer if we retype it like this:

$associative_array = array(
    'product_id' => $product_id ,
    'outbiddeduser_id' => $outbiddeduser,
    'log_id' => $log_id
);
do_action( 'woocommerce_simple_auctions_outbid',  $associative_array );

Thus:

add_action('woocommerce_simple_auctions_outbid', 'test', 10, 1);
function test( $associative_array ) {

It comes in as an array, because that's what you passed through. There's no magic unpacking of the array in do_action

  • I forgot to mention that I also tried what you suggested. – Tahi Reu Sep 3 '19 at 17:32
  • I edited my question now. – Tahi Reu Sep 3 '19 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.