'Get WooCommerce product variation attribute terms in admin products general box

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

enter image description here

The general tab

enter image description here

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 } ?>


Solution 1:[1]

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.

Solution 2:[2]

I display the id and sku of each variable product in the general tab.

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

// 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">

With

// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
    $count++;
    
    $child = wc_get_product( $child_id );
        
    $pr_attribute_name = implode( " / ", $child->get_variation_attributes() );          
    $pr_id_variable = $child->get_id();
    $pr_sku_variable = $child->get_sku();
    
?>

<p> <?php echo $pr_attribute_name; ?> </p>
<p class="form-field">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2