I display the id and sku of each variable product in the general tab – of the wp admin product data box with the following code. Any idea how to get the the variable product attribute name too – in this case "test 1" and "test 2"?
The variations
The general tab
The code
<?php
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
$children_ids = wc_get_product()->get_children();
$count = 0;
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$pr_id_variable = wc_get_product($child_id)->get_id();
$pr_sku_variable = wc_get_product($child_id)->get_sku();
?>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> ID</label>
<input type="text" value="ID<?php echo $pr_id_variable; ?>"></input>
</p>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> SKU</label>
<textarea><?php echo $pr_sku_variable; ?></textarea>
</p>
<?php } ?>
<?php } ?>
2
Answers
In WooCommerce, the general tab is not available (visible) for a variable product, except when taxes enabled. Under WooCommerce > Settings > General > Enable taxes.
Because it’s used to set parent tax class and tax status so if you don’t have taxes enabled, nothing will show there.
To answer your question
Replace this part
With
For variation attribute names (variation Id and sku), you can use the following:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.