WooCommerce:如何在管理产品列表中的产品名称旁边添加产品类型

bha*_*anu 6 php wordpress product woocommerce hook-woocommerce

我创建了一个新的产品类型“ Crush Video Product”。它从其自定义选项卡正确保存所有元字段。

// 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';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我见过一些插件,其中产品类型的名称写在管理区域的产品名称后面,您可以在其中看到所有产品。

在此输入图像描述

我搜索了很多,但找不到一个钩子来做到这一点。

7uc*_*f3r 5

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

  • 渲染列:名称。

似乎缺少一个更合适的钩子,所以一种方法是,manage_product_posts_custom_column如果需要的话,使用一些 CSS 来显示

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 );
Run Code Online (Sandbox Code Playgroud)

结果:

产品名称旁边的产品类型