簡體   English   中英

WooCommerce:如何在管理產品列表中的產品名稱旁邊添加產品類型

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

我創建了一個新的產品類型“ 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';
        }
    }
}

我在管理區域看到了產品類型名稱寫在產品名稱之后的插件,您可以在其中查看所有產品。

在此處輸入圖像描述

我搜索了很多,但找不到這樣做的鈎子。

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 );

結果:

產品名稱旁邊的產品類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM