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

Is there a way when adding a new product that all available Attributes would automatically be added to this product and oppened/expanded so that when I’m adding new product I don’t have to first add them and then click on the headers to expand them?

Hope this image clears what I want to achieve:
enter image description here

Sure, I can manually trigger the click on “Expand all” button with jQuery, but am having trouble finding the place where to add this piece of jQ code. More important to me is the former question though, so any push in the right direction is much appreciated.

edit: I managed to do the open(show) part of the problem by adding the line:

jQuery('.expand_all').trigger("click");

in the themes/mytheme/admin/admin-functions.php file in siteoptions_admin_head function. But the first qq still remains. I did find a work around though – I could first add one product and add every existing attribute to it, and when making new products I would make a copy of this product and them add/remove attributes I don’t need. This however would not work dynamically if attributes change, so its not much of an improvement.

2 Answers
2

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