0
votes

In general I want this:

function parent () {
    $foo = 'something';
    do_action ('hook');
}

function child () {
    echo $foo;
}

add_action ('hook','child');

In particular I want to print the WooCommerce product $attributes variable used to make attribute selection drop-down to get the idea what's inside the array and later manipulate this data. The code is:

function 'print_attributes' () {
    global $woocommerce, $attributes;
    print '<pre>';
    print_r ($attributes);
    print '</pre>';
}

add_action('woocommerce_before_add_to_cart_form','print_attributes');

<pre> tags are printed on product page but $attributes are empty. As far as I understand my code won't work until I declare global $attributes in parent function as well which means modifying WooCommerce template files which I want to avoid.

$attributes are called in variable.php template - https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/add-to-cart/variable.php#L22, the hook is placed a few lines later - https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/add-to-cart/variable.php#L26. Is there any way I can access the variable without modifying WooCommerce code?

I understand I can just override template via child theme, but I am eager to know if this can be done via hook as I'm just learning PHP and Wordpress.

1
You should just use instead: global $product; $attributes = $product->get_attributes(); print_r( $attributes ); inside your print_attributes() function.LoicTheAztec
Thank you for the suggestion. This will work in this situation, but I want to know if there's a way to do this kind of trick in general. If it is possible to manipulate some variable from parent function via hook. The information in scope section of php website is rather scarce.pacmanito
Yes that is possible, but each case will use different hooks ans ways…LoicTheAztec
Can you please elaborate on this. So far most hooks I used have no parameters to pass.pacmanito
Sorry, but most of them have parameters… Action hooks are to trigger an action when a hook is called… Filter hooks are made to change something, like an argument (a variable) that is always returned at the end in a hooked function… Your question is not enough clear as you don't explain what you are trying to do and for what purpose…LoicTheAztec

1 Answers

0
votes

using your demo code:

function parent () {
    $foo = 'something';
    do_action ('hook', $foo);
}

function child ($foo) {
    echo $foo;
}

add_action ('hook','child');

You can pass variables into the hook, by default the hook pass the first variable to the executed function, if you need more use like this:

function parent () {
    $foo = 'something';
    $bar = 'something';
    $baz = 'something';
    do_action ('hook', $foo, $bar, $baz);
}

function child ($foo, $bar, $baz) {
    echo $foo;
    echo $bar;
    echo $baz;
}

add_action ('hook','child', 10, 3);

find more here: https://developer.wordpress.org/reference/functions/add_action/