How to automatically add and show(open) all the product attributes?

This was not as easy as I initially thought.

This will show show every attribute…. apparently by default WC hides any attribute that doesn’t have any terms. They’re already in the code, just with a display:none;.

function wpa_120062_scripts(){ ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('.woocommerce_attributes > div').removeClass('closed').show();
            $('#woocommerce_attributes a.expand_all').click();
        });

    </script>
<?php
}
add_action('admin_print_footer_scripts', 'wpa_120062_scripts');

Depending on whether you plan to click all the “Visible on the product page” checkboxes, you can add some data to the attributes for a particular post when it is created. Per some other WPA questions, I’ve hooked into transition_post_status. The “auto-draft” status, supposedly only fires on first creation… from there it either goes to “draft” or any of the other post statuses.

function wpa_120062_new_product($new_status, $old_status, $post){
    if ( $new_status == "auto-draft" && isset( $post->post_type ) && $post->post_type == 'product' ){

        // do stuff here
            $defaults = array ( 'pa_color' => array (
                                          'name' => 'pa_color',
                                          'value' => '',
                                          'position' => 1,
                                          'is_visible' => 1,
                                          'is_variation' => 1,
                                          'is_taxonomy' => 1,
                                       ),
                                'pa_capacity' => array (
                                          'name' => 'pa_capacity',
                                          'value' => '',
                                          'position' => 2,
                                          'is_visible' => 1,
                                          'is_variation' => 1,
                                          'is_taxonomy' => 1,
                                       )
            );

        update_post_meta( $post->ID , '_product_attributes', $defaults );

   }

}
add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);

pa_capacity and pa_color are just some sample attributes that I already had installed.

PS- I would suggest updating wpa_120062_scripts to only display on product edit screens, but I’m a little fried after that.

EDIT

We can use the woocommerce function wc_get_attribute_taxonomies() to dynamically get all the attributes and loop through them:

function wpa_120062_new_product($new_status, $old_status, $post){

    if ( $new_status == "auto-draft" && isset( $post->post_type ) && $post->post_type == 'product' ){

        if( function_exists( 'wc_get_attribute_taxonomies' ) && ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) ) {

            $defaults = array();

            foreach ( $attribute_taxonomies as $tax ) {

                $name = wc_attribute_taxonomy_name( $tax->attribute_name );

                // do stuff here
                $defaults[ $name ] = array (
                    'name' => $name,
                    'value' => '',
                    'position' => 1,
                    'is_visible' => 1,
                    'is_variation' => 1,
                    'is_taxonomy' => 1,
                );

            update_post_meta( $post->ID , '_product_attributes', $defaults );

            }

        }

    }

}
add_action('transition_post_status', 'wpa_120062_new_product', 10, 3);

PS- I think the defaults are going to be a little different in WC2.1. This isn’t quite working the same for me any longer with my bleeding edge git versions.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)