• Resolved ehmarketing

    (@ehmarketing)


    Hi there,

    I’d like to add a column on the Product Admin page to show what Attributes each product has. Chiefly I’d like a quick and easy way to look over all the products and see any I might have forgotten to put attributes on or edit others.

    Any help on how to achieve this would be much appreciated!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The filter manage_edit-{post_type}_columns is used to actually add a column. To control what is displayed in the column for each post (product), you can use the manage_{post_type}_posts_custom_column action. This action is called for each custom column for every post, and it passes two arguments: $column and $postid.

    Add the column ‘header’

    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );

    Add the column ‘content’

    function add_product_column_content( $column, $postid ) {
        if ( $column == 'new_column' ) {	
            echo 'content';
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );

    However, because we have access to the $postid via the hook, can we do this

    function add_product_column_content( $column, $postid ) {	
        if ( $column == 'new_column' ) {
            // Get product object
            $product = wc_get_product( $postid );
    	
            // Get product type
            $product_type = $product->get_type();
    
            //Get product name
            $product_name = $product->get_name();
    		
            echo $product_name . ' is a ' . $product_type . ' type';
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );

    or for in this case

    // Get Product Variations
    $product->get_attributes();
    $product->get_default_attributes();

    Regards

    Thread Starter ehmarketing

    (@ehmarketing)

    Hi there,

    Thanks very much for this. I’ve tried putting it in Codesnippets but I’m not getting a new column appearing.

    I’ve put the code in exactly as:

    
    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
    
    function add_product_column_content( $column, $postid ) {	
        if ( $column == 'new_column' ) {
            // Get Product Variations
    		$product->get_attributes();
    		$product->get_default_attributes();
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
    

    Have I got something wrong?

    Hi,

    Give it a try this way, is this what you were looking for?

    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
    
    function add_product_column_content( $column, $postid ) {
        if ( $column == 'new_column' ) {
            // Get product object
            $product = wc_get_product( $postid );
    		
            // Get Product Variations
            $product_attributes = $product->get_attributes();
    		
            foreach ( $product_attributes as $product_attribute ) {			
                $attribute_options = $product_attribute->get_options();
    			
                foreach ( $attribute_options as $attribute_option ) {
                    echo $attribute_option . '<br>';
                }
            }
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
    Thread Starter ehmarketing

    (@ehmarketing)

    That’s great! It has created a new column which I have renamed, and is pulling in the attributes. It’s pulling in the attributes as numbers, is there something I can add to have it show the actual attribute name?

    Many thanks again for your help, really appreciated!

    Do you mean this?

    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
    
    function add_product_column_content( $column, $postid ) {
        if ( $column == 'new_column' ) {
            // Get product object
            $product = wc_get_product( $postid );
    		
            // Get Product Variations
            $product_attributes = $product->get_attributes();
    		
            foreach ( $product_attributes as $product_attribute ) {
                $attribute_name = $product_attribute->get_name();
    			
                echo $attribute_name;
            }
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
    Thread Starter ehmarketing

    (@ehmarketing)

    Awesome, that has very very nearly done it!

    It is bringing in the names of the attributes now, but each attribute is prexifed by pa_, like this “pa_colourpa_treatmentpa_plant-type”.

    It is also showing me which attributes have been used, but not what terms have been applied. e.g. above it says colour, but not what colour term (blue, red) has been selected.

    The following code will print an array that contains all information about the attribute. With the 2 examples above from me you might be able to print the necessary information yourself?

    If this fails to come up with a solution yourself, then make a sreenshot to show what information you want to exactly display from the array.

    regards

    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
    
    function add_product_column_content( $column, $postid ) {
        if ( $column == 'new_column' ) {
            // Get product object
            $product = wc_get_product( $postid );
    		
            // Get Product Variations
            $product_attributes = $product->get_attributes();
    		
            echo '<pre>', print_r($product_attributes, 1), '</pre>';
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
    Thread Starter ehmarketing

    (@ehmarketing)

    Hi there, thanks again for your help with this!

    I’m not trying to print anything – or does ‘print’ in this context mean something else?

    Your code above helped to get the Attributes column to appear and fill with the Attribute name (e.g ‘Colour’), now I hope to include the term (e.g. ‘Red’), and remove the ‘pa_’ that seems to be appearing as a prefix in the terms.

    i.e now it displays “pa_colourpa_treatment”, I’d like to get “colour:red, treatment:dyed”.

    Well your setup is clearly slightly different than mine (view attached image). That is why I ask you to print the array (echo) so that you can read and use the necessary information. If this is not possible to achieve a result yourself, then make a sreenshot, as I have already asked. Otherwise I don’t know what it looks like in your setup. And we will continue to deal with your question without really achieving a result.

    https://pasteboard.co/IPWcGf9.jpg

    Thread Starter ehmarketing

    (@ehmarketing)

    Ah now I understand, so I have put in the code you gave above and here is a screenshot of the array: https://ibb.co/Hp3PBnc and https://ibb.co/XXWzzbM

    I wouldn’t know what to do with this information!

    Hi,

    To change the ‘name’ will not be a problem, for this you can indeed use a function that will remove the prefix. However, I also see numbers with the options and no color names. Could it be that each number has to represent a certain color? do you have many different colors?

    If there aren’t that many colors you could say with code number 159 = red, number 73 = blue…

    If it is about many different colors, it seems interesting to me to know where those numbers come from. Is it an id of a color? because then you could, for example, read the color names based on the id with code.

    Can you tell something more about that?

    You can remove the prefix from the following example

    https://www.ads-software.com/support/topic/adding-product-attributes-column-to-product-admin-page/#post-12323231

    Through
    echo $attribute_name;

    To be replaced by
    echo str_replace( 'pa_', '', $attribute_name );

    Thread Starter ehmarketing

    (@ehmarketing)

    Hi @crslz

    There are 13 different colours terms for the colour Attribute, and at the moment there are 15 different attributes. Possibly over 100 terms in total.

    All have been set up the standard way in Woocommerce Attributes and ‘configure terms’. I have no idea how it works but my guess is that the numbers refer to colours in the same way that categories and posts etc all have numbers. I haven’t set the numbers, that happens automatically. I will see if I can find what number each term has!

    Thanks again!

    Just as you did with the array, you could also try printing the next piece of code. This will print all data about the product object, maybe you can find the color from that information?

    If not, you will have to look for a developer who can gain access to the backend of your website in order to be able to view it more closely.

    Regards

    function add_product_column( $columns ) {
        //add column
        $columns['new_column'] = __( 'New column', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
    
    function add_product_column_content( $column, $postid ) {
        if ( $column == 'new_column' ) {
            // Get product object
            $product = wc_get_product( $postid );
    		
            echo '<pre>', print_r($product, 1), '</pre>';
        }
    }
    add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
    Thread Starter ehmarketing

    (@ehmarketing)

    Thanks again for all your help with this!

    Hi there!

    It sounds like this has a solid ending, so I am going to close this thread. If you have other questions please let us know in a new thread!

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Adding Product Attributes column to Product Admin page’ is closed to new replies.