简体   繁体   English

在 Woocommerce 的管理产品列表中添加自定义“销售价格”列

[英]Add a custom "Sale Price" column to admin products list in Woocommerce

I am trying to add a Sale Price column to admin Products list in Woocommerce.我正在尝试在 Woocommerce 的管理产品列表中添加一个销售价格列。

Here is my code:这是我的代码:

   add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);

   function onsale_product_column($columns){
   $new_columns = [];
   foreach( $columns as $key => $column ){
      $new_columns[$key] = $columns[$key];
      if( $key == 'product_cat' ) {
           $new_columns['onsale'] = __( 'Sale Price','woocommerce');
       }
     }
    return $new_columns;
    }

    add_action( 'manage_product_posts_custom_column', 
    'onsale_product_column_content', 10, 2 );

    function onsale_product_column_content( $column, $product_id ){
    global $post;
    if( $column == 'onsale' ){
        if( $product_id->is_on_sale() ) {
            echo $product_id->get_sale_price();
        }
        echo '-';
    }
}

But it doesn't work.但它不起作用。 What I am doing wrong?我做错了什么?

There is some mistakes in your code… Try the following instead:您的代码中有一些错误……请尝试以下操作:

add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
    $new_columns = [];
    foreach( $columns as $key => $column ){
        $new_columns[$key] = $columns[$key];
        if( $key == 'product_cat' ) {
            $new_columns['onsale'] = __( 'Sale Price','woocommerce');
        }
    }
    return $new_columns;
}

add_action( 'manage_product_posts_custom_column', 'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $post_id ){
    if( $column == 'onsale' ){
        global $post, $product;

        // Excluding variable and grouped products
        if( is_a( $product, 'WC_Product' ) && ! $product->is_type('grouped') &&
        ! $product->is_type('variable') && $product->is_on_sale() ) {
            echo strip_tags( wc_price( $product->get_sale_price() ) );
        }
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and work.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM