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 } ?>
For variation attribute names (variation Id and sku), you can use the following:
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
global $product_object;
if( $product_object->is_type('variable') ) {
$count = 1;
foreach( $product_object->get_children() as $variation_id ) {
$variation = wc_get_product($variation_id);
$name = array();
foreach( $variation->get_attributes() as $taxonomy => $term_slug ) {
$name[] = get_term_by( 'slug', $term_slug, $taxonomy )->name;
}
echo '<p><strong>'. sprintf( __("Variation %s Name: %s", "woocommerce"), $count, '</strong>' . implode(' - ', $name) ) .'</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s ID", "woocommerce"), $count) . '</label>
<input type="text" value="ID' . $variation_id . '"></input>
</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s SKU", "woocommerce"), $count) . '</label>
<textarea>' . $variation->get_sku() . '</textarea>
</p>';
$count++;
}
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Note: this works for product attribute taxonomies only (starting with "pa_"), but not with custom attributes.