AI Verified

Name

Add WooCommerce Attributes as Admin Column

About

When. you're using Attributes, make it easier to see if any are active from your Products Dashboard.

Language

PHP

Rating

Voted: 1 by 1 user(s)

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

10/05/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Add WooCommerce Attributes as Admin Column

 
                    
1function add_product_attributes_column( $columns ) {
2 // Add a new column for attributes after the 'name' column
3 $new_columns = array();
4 foreach ( $columns as $key => $title ) {
5 $new_columns[ $key ] = $title;
6 if ( 'name' === $key ) {
7 $new_columns['product_attributes'] = __( 'Attributes', 'your-text-domain' );
8 }
9 }
10 return $new_columns;
11}
12add_filter( 'manage_edit-product_columns', 'add_product_attributes_column', 10, 1 );
13 
14// Populate the custom column with product attributes
15function show_product_attributes_in_column( $column, $postid ) {
16 if ( $column == 'product_attributes' ) {
17 global $product;
18 // Ensure we have a product object
19 if ( !is_a( $product, 'WC_Product' ) ) {
20 $product = wc_get_product( $postid );
21 }
22 
23 if ( ! $product ) {
24 echo '';
25 return;
26 }
27 
28 // Get the product attributes
29 $attributes = $product->get_attributes();
30 
31 if ( !empty( $attributes ) ) {
32 $attributes_list = array();
33 foreach ( $attributes as $attribute ) {
34 // If the attribute is a taxonomy
35 if ( $attribute->is_taxonomy() ) {
36 $values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'names' ) );
37 $attributes_list[] = wc_attribute_label( $attribute->get_name() ) . ': ' . join( ', ', $values );
38 } else {
39 // For custom product attributes
40 $attributes_list[] = wc_attribute_label( $attribute->get_name() ) . ': ' . $attribute->get_options();
41 }
42 }
43 echo join( ', ', $attributes_list );
44 } else {
45 echo __( 'No attributes', 'your-text-domain' );
46 }
47 }
48}
49add_action( 'manage_product_posts_custom_column', 'show_product_attributes_in_column', 10, 2 );

1

Comments

  • MM

    2 months ago

    Seeing the attributes right there is very helpful

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

WooCommerce Display Product-Unique Content On Loop Items

Added: 1 year ago

Last Updated: 1 year ago

This guide shows you how you can display product-specific content on the loop items which are shown on the product archive page. You may wish to show an offer you are running or the billing cycle for...

WooCommerce

AI Verified

0

WordPress Create Custom User Role

Added: 1 year ago

Last Updated: 1 year ago

This code snippet creates a custom role showing every possible default capability.

WooCommerce

AI Verified

1

WooCommerce Review Form Visual Hooks Guide

Added: 1 year ago

Last Updated: 2 weeks ago

The WooCommerce review form uses the default WordPress comment form and adds a few sprinkles of meta data, the star rating being one of these meta fields. This guide gives you a visual insight into wh...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

33

Remove Unused Javascript

Added: 1 year ago

Last Updated: 2 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 3 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 2 months ago

This can be used for any WordPress Builder to aid working with CSS Grids.