Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
Thank you for your help! It turns out that with first jQuery code I solved my "problem" - all of the attributes are now shown by default when I'm adding a new product (true, when editing also). I added this line of code to the wpa_120062_scripts function $('input[name^="attribute_visibility"]').prop('checked', true); which automatically checks all of the "Visible on the product page" checkboxes. Out of curiosity - would it be possible to mark these checkboxes only on the first Add product?, because now if I uncheck some checkboxes and publish and edit again they will be all checked again. –  Nikola Oct 30 '13 at 8:52
    
@Nikola that's what my second function should do. is_visible is set to 1 but only when the new product is created. Thereafter you can toggle it off as you like. I'm not sure there is a way to check the post status from JS to make your .prop() work. –  helgatheviking Oct 30 '13 at 10:08
    
Yes, true, but I would then need to somehow go through all of the possible attributes as I would want to set every one of them to is_visible and ofc it could happen that some attributes would be added/removed in a latter stage. I did try that with get_post_meta ($post->ID) but am getting an empty array. I guess that's logical as the post itself is not yet "in" the db at the time of creating as I understand this, but then again I could be wrong so a general push in the right direction would be appreciated. –  Nikola Oct 30 '13 at 10:20
    
It depends on where you are trying to use get_post_meta() I think at this point (for example, in my function above $post is passed as a parameter. Sometimes you have to global $post if it isn't. If you don't want to hard-code a list of $defaults you might be able to use get_taxonomies() and run through a loop? –  helgatheviking Oct 30 '13 at 10:38
    
Yes, thanks - I managed to get them by using get_taxonomies(). But this now made me discover another weird thing; the update_post_meta( $post->ID , '_product_attributes', $defaults ); is not updating anything in my attributes. Could it be I'm missing something? I've been googling and haven't found this '_product_attributes' option, as I'm suspecting this is the part that goes wrong/doesnt' update the correct attributes/anything. –  Nikola Oct 30 '13 at 12:44

WC 2.1 update:

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

</script>

share|improve this answer

protected by Community Apr 23 at 11:17

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.