如何在WooCommerce管理产品列表中显示“运费类别”列

huangapple go评论61阅读模式
英文:

How to show "Shipping Class" column in WooCommerce admin product list

问题

我正在使用 WooCommerce。

我知道您可以在“所有产品”页面上显示:图片、SKU、库存、价格、类别、标签、特色和日期。我已经四处寻找,但找不到在页面上添加“运输类别”列的方法。

上面的代码显示了商品是否可见。有人能指导我如何显示产品的运输类别吗?

谢谢。

英文:

I am using woocommerce.

I know that you can show: Image, SKU, Stock, Price, Categories, Tags, Featured, and Date on the "All Products" page. I have had a look around I cannot find a way to add a "Shipping Class" column to the page.

 * @snippet       New Products Table Column @ WooCommerce Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'manage_edit-product_columns', 'bbloomer_admin_products_visibility_column', 9999 );
 
function bbloomer_admin_products_visibility_column( $columns ){
   $columns['visibility'] = 'Visibility';
   return $columns;
}
 
add_action( 'manage_product_posts_custom_column', 'bbloomer_admin_products_visibility_column_content', 10, 2 );
 
function bbloomer_admin_products_visibility_column_content( $column, $product_id ){
    if ( $column == 'visibility' ) {
        $product = wc_get_product( $product_id );
      echo $product->get_catalog_visibility();
    }
}

The code above shows if an item is visible or not. Can anyone point me in the right direct to show the product's shipping class?

Thanks

答案1

得分: 2

为了在管理产品列表中显示运输类别,请使用以下修改后的代码:

add_filter('manage_edit-product_columns', 'add_admin_products_shipping_class_column', 999);
function add_admin_products_shipping_class_column($columns){
   $columns['shipping_class'] = __('运输类别', 'woocommerce');
   return $columns;
}

add_action('manage_product_posts_custom_column', 'get_admin_products_shipping_class_column_content', 10, 2);
function get_admin_products_shipping_class_column_content($column, $product_id){
    if ($column === 'shipping_class') {
        global $post, $product;

        if (is_a($product, 'WC_Product')) {
            $shipping_class_id = (int) $product->get_shipping_class_id();
            $shipping_class    = get_term($shipping_class_id, 'product_shipping_class');

            echo !is_wp_error($shipping_class) ? $shipping_class->name : '<em>n/a</em>';
        }
    }
}

此代码应添加到活动子主题(或活动主题)的functions.php文件中。已测试并可正常运行。

英文:

To show the shipping class in Admin product list, use the following revisited code:

add_filter( &#39;manage_edit-product_columns&#39;, &#39;add_admin_products_shipping_class_column&#39;, 999 );
function add_admin_products_shipping_class_column( $columns ){
   $columns[&#39;shipping_class&#39;] = __(&#39;Shipping Class&#39;, &#39;woocommerce&#39;);
   return $columns;
}
 
add_action( &#39;manage_product_posts_custom_column&#39;, &#39;get_admin_products_shipping_class_column_content&#39;, 10, 2 );
function get_admin_products_shipping_class_column_content( $column, $product_id ){
    if ( $column === &#39;shipping_class&#39; ) {
        global $post, $product;

        if( is_a( $product, &#39;WC_Product&#39; ) ) {
            $shipping_class_id = (int) $product-&gt;get_shipping_class_id();
            $shipping_class    = get_term($shipping_class_id, &#39;product_shipping_class&#39;);
            
            echo ! is_wp_error($shipping_class) ? $shipping_class-&gt;name : &#39;&lt;em&gt;n/a&lt;/em&gt;&#39;;
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

huangapple
  • 本文由 发表于 2023年6月18日 18:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定