Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce: How to add product type next to product name in admin product list

I have created a new product type "Crush Video Product". It is saving all the meta fields correctly from its custom tab.

// add a product type
add_filter( 'product_type_selector', 'crush_add_custom_product_type' );

function crush_add_custom_product_type( $types ){
    $types[ 'crush_video_product' ] = __( 'Group Video Class' );
    return $types;
}

// Initiate Class when plugin is loaded
add_action( 'plugins_loaded', 'crush_create_custom_product_type' );

function crush_create_custom_product_type(){
    // declare the product class

    class WC_Product_Crush_Video_Product extends WC_Product{
        public function __construct( $product ) {
            $this->product_type = 'crush_video_product';
            parent::__construct( $product );
            // add additional functions here
        }

        // Needed since Woocommerce version 3
        public function get_type() {
            return 'crush_video_product';
        }
    }
}

I have seen plugins where the name of the product type is written after the name of the product in the admin area where you can see all the products.

enter image description here

I searched a lot, but couldn't find a hook to do this.

like image 232
bhanu Avatar asked Oct 16 '22 03:10

bhanu


1 Answers

https://github.com/woocommerce/woocommerce/blob/4.1.0/includes/admin/list-tables/class-wc-admin-list-table-products.php#L157-L203

  • Render column: name.

A more suitable hook seems to be missing, so one way is, use manage_product_posts_custom_columnand if desired some CSS for the display

function action_manage_product_posts_custom_column( $column, $postid ) {        
    if ( $column == 'name' ) {
        // Get product
        $product = wc_get_product( $postid );
        
        // Get type
        $product_type = $product->get_type();
        
        // Output
        echo '&nbsp;<span>&ndash; ' .  ucfirst( $product_type ) . '</span>';
    }
}
add_action( 'manage_product_posts_custom_column', 'action_manage_product_posts_custom_column', 20, 2 );

Result:

Product type next to product name

like image 169
7uc1f3r Avatar answered Oct 21 '22 02:10

7uc1f3r