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.
global $product; $attributes = $product->get_attributes(); print_r( $attributes );
inside yourprint_attributes()
function. – LoicTheAztec