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';
}
}
}
我见过一些插件在管理区的产品名称后面写着产品类型的名称,在那里你可以看到所有的产品。
我搜索了很多,但找不到执行此操作的钩子。
- Render column: name.
似乎缺少更合适的挂钩,所以一种方法是,使用 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 ' <span>– ' . ucfirst( $product_type ) . '</span>';
}
}
add_action( 'manage_product_posts_custom_column', 'action_manage_product_posts_custom_column', 20, 2 );
结果:
我创建了一个新的产品类型“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';
}
}
}
我见过一些插件在管理区的产品名称后面写着产品类型的名称,在那里你可以看到所有的产品。
我搜索了很多,但找不到执行此操作的钩子。
- Render column: name.
似乎缺少更合适的挂钩,所以一种方法是,使用 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 ' <span>– ' . ucfirst( $product_type ) . '</span>';
}
}
add_action( 'manage_product_posts_custom_column', 'action_manage_product_posts_custom_column', 20, 2 );
结果: