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

标签 php wordpress woocommerce product 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';
        }
    }
}

我在管理区域看到了产品类型名称写在产品名称之后的插件,您可以在其中查看所有产品。

enter image description here

我搜索了很多,但找不到这样做的钩子(Hook)。

最佳答案

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.

似乎缺少一个更合适的钩子(Hook),所以一种方法是,使用 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 );

结果:
Product type next to product name

关于php - WooCommerce:如何在管理产品列表中的产品名称旁边添加产品类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62301223/

相关文章:

PHP mysql查询语法错误

PHP 更改为 mysqli。 mysqli_connection 不是全局的吗?

php - 如何在 Wordpress 中获取当前选定的菜单项

javascript - Wordpress 推荐 slider 及其与页脚的链接

css - css 中的 Wordpress/OptionTree 引用

php - 显示 INNER JOIN 数据与 DATE_FORMAT 数据尚未工作

php - 新手: php how to read in file names in order of filename

woocommerce - Woocommerce,从版本ID获取版本名称

php - 应用优惠券代码后更改 WooCommerce 购物车价格

wordpress - 在 WooCommerce 购物车中添加产品元值作为费用